문제

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top