Pergunta

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

Foi útil?

Solução

You could do something like this:

floor(sqrt(id)) = sqrt(id)

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top