How to determine whether the number is float or integer in SQL Server?

后端 未结 4 1353
[愿得一人]
[愿得一人] 2021-02-20 13:17

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         


        
4条回答
  •  遥遥无期
    2021-02-20 13:33

    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

提交回复
热议问题