Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether the number is float or integer

I need to write this query in SQL Server:

IF isFloat(@value) = 1
BEGIN
    PRINT 'this is float number'
END
ELSE
BEGIN
    PRINT 'this is integer number'
END

Please help me out with this, thanks.

like image 441
Azeem Avatar asked Oct 13 '11 10:10

Azeem


2 Answers

declare @value float = 1

IF FLOOR(@value) <> CEILING(@value)
BEGIN
    PRINT 'this is float number'
END
ELSE
BEGIN
    PRINT 'this is integer number'
END
like image 119
Martin Smith Avatar answered Sep 28 '22 04:09

Martin Smith


Martin, under certain circumstances your solution gives an incorrect result if you encounter a value of 1234.0, for example. Your code determines that 1234.0 is an integer, which is incorrect.

This is a more accurate snippet:

if cast(cast(123456.0 as integer) as varchar(255)) <> cast(123456.0 as varchar(255)) 
begin 
  print 'non integer' 
end 
else 
begin 
  print 'integer' 
end

Regards,

Nico

like image 29
Nico van Niekerk Avatar answered Sep 28 '22 03:09

Nico van Niekerk