我需要在sql server中编写这个查询:
IF isFloat(@value) = 1
BEGIN
PRINT 'this is float number'
END
ELSE
BEGIN
PRINT 'this is integer number'
END
请帮助我,谢谢。
答案 0 :(得分:39)
declare @value float = 1
IF FLOOR(@value) <> CEILING(@value)
BEGIN
PRINT 'this is float number'
END
ELSE
BEGIN
PRINT 'this is integer number'
END
答案 1 :(得分:2)
Martin,在某些情况下,如果遇到1234.0的值,则解决方案会给出错误的结果。您的代码确定1234.0是一个整数,这是不正确的。
这是一个更准确的代码段:
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
此致
尼科
答案 2 :(得分:0)
DECLARE @value FLOAT = 1.50
IF CONVERT(int, @value) - @value <> 0
BEGIN
PRINT 'this is float number'
END
ELSE
BEGIN
PRINT 'this is integer number'
END
答案 3 :(得分:0)
查看以下代码是否有帮助。在以下值只有9, 2147483647,1234567符合Integer标准。我们可以创建它 功能,可以使用它。
CREATE TABLE MY_TABLE(MY_FIELD VARCHAR(50))
INSERT INTO MY_TABLE
VALUES('9.123'),('1234567'),('9'),('2147483647'),('2147483647.01'),('2147483648'), ('2147483648ABCD'),('214,7483,648')
SELECT *
FROM MY_TABLE
WHERE CHARINDEX('.',MY_FIELD) = 0 AND CHARINDEX(',',MY_FIELD) = 0
AND ISNUMERIC(MY_FIELD) = 1 AND CONVERT(FLOAT,MY_FIELD) / 2147483647 <= 1
DROP TABLE MY_TABLE
OR
DECLARE @num VARCHAR(100)
SET @num = '2147483648AS'
IF ISNUMERIC(@num) = 1 AND @num NOT LIKE '%.%' AND @num NOT LIKE '%,%'
BEGIN
IF CONVERT(FLOAT,@num) / 2147483647 <= 1
PRINT 'INTEGER'
ELSE
PRINT 'NOT-INTEGER'
END
ELSE
PRINT 'NOT-INTEGER'