You could do something like this:
floor(sqrt(id)) = sqrt(id)
题
How can I do this in T-Sql:
SQRT(id) % 1 = 0
I can't cast the result of the Sqrt() function because this renders the logic above useless. Any Ideas on how I can achieve this?
Thanks
解决方案
You could do something like this:
floor(sqrt(id)) = sqrt(id)
其他提示
To see whether the sqrt is whole, try this: SQRT(id) * SQRT(id) = id
. This might run into floating point precision issues though. I think this fixes them reliably for all numbers up to a certain threshold:
CONVERT(INT, SQRT(id)) * CONVERT(INT, SQRT(id)) = id
After the threshold, the precision will not be enough. You'll see false negatives but never false positives.
You could cast it to a varchar
and then take the substring
after the decimal point. More info here: http://dbaspot.com/sqlserver-programming/411976-how-get-numbers-after-decimal-tsql.html