检查一个varchar是否是一个数字(TSQL)
有一个简单的方法来弄清楚,如果一个varchar是一个数字?
例子:
abc123 – >没有号码
123 – >是的,它是一个数字
谢谢 :)
ISNUMERIC会做
在文章中也检查NOTES部分。
ISNUMERIC不会这样做 – 它告诉你,string可以转换成任何数字types,这几乎总是一个毫无意义的信息知道。 例如,根据ISNUMERIC,以下所有内容都是数字:
£,$,0d0
如果你想检查数字和唯一的数字,一个否定的LIKEexpression式是你想要的:
not Value like '%[^0-9]%'
你可以这样检查
declare @vchar varchar(50) set @vchar ='34343'; select case when @vchar not like '%[^0-9]%' then 'Number' else 'Not a Number' end
使用SQL Server 2012+,如果您有特定的需求,可以使用TRY_ *函数。 例如,
-- will fail for decimal values, but allow negative values TRY_CAST(@value AS INT) IS NOT NULL -- will fail for non-positive integers; can be used with other examples below as well, or reversed if only negative desired TRY_CAST(@value AS INT) > 0 -- will fail if a $ is used, but allow decimals to the specified precision TRY_CAST(@value AS DECIMAL(10,2)) IS NOT NULL -- will allow valid currency TRY_CAST(@value AS MONEY) IS NOT NULL -- will allow scientific notation to be used like 1.7E+3 TRY_CAST(@value AS FLOAT) IS NOT NULL
我碰到需要允许十进制值,所以我not Value like '%[^0-9.]%'
使用not Value like '%[^0-9.]%'
Wade73的小数的答案不起作用。 我已经修改它只允许一个小数点。
declare @MyTable table(MyVar nvarchar(10)); insert into @MyTable (MyVar) values (N'1234') , (N'000005') , (N'1,000') , (N'293.8457') , (N'x') , (N'+') , (N'293.8457.') , (N'......'); -- This shows that Wade73's answer allows some non-numeric values to slip through. select * from ( select MyVar , case when MyVar not like N'%[^0-9.]%' then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber; -- Notice the addition of "and MyVar not like N'%.%.%'". select * from ( select MyVar , case when MyVar not like N'%[^0-9.]%' and MyVar not like N'%.%.%' then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber;
Damien_The_Unbeliever指出,他只对数字有利
Wade73增加了一点来处理小数点
neizan做了一个额外的调整,因为没有其他地方
不幸的是,似乎没有处理负面价值,他们似乎有一个值逗号问题…
这里是我调整负值和逗号
declare @MyTable table(MyVar nvarchar(10)); insert into @MyTable (MyVar) values (N'1234') , (N'000005') , (N'1,000') , (N'293.8457') , (N'x') , (N'+') , (N'293.8457.') , (N'......') , (N'.') , (N'-375.4') , (N'-00003') , (N'-2,000') , (N'3-3') , (N'3000-') ; -- This shows that Neizan's answer allows "." to slip through. select * from ( select MyVar , case when MyVar not like N'%[^0-9.]%' then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber; -- Notice the addition of "and MyVar not like '.'". select * from ( select MyVar , case when MyVar not like N'%[^0-9.]%' and MyVar not like N'%.%.%' and MyVar not like '.' then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber; --Trying to tweak for negative values and the comma --Modified when comparison select * from ( select MyVar , case when MyVar not like N'%[^0-9.,-]%' and MyVar not like '.' and isnumeric(MyVar) = 1 then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber;
DECLARE @A nvarchar(100) = '12' IF(ISNUMERIC(@A) = 1) BEGIN PRINT 'YES NUMERIC' END
Neizan的代码只允许“。”的值。 通过。 过于迂腐的风险,我添加了一个AND
子句。
declare @MyTable table(MyVar nvarchar(10)); insert into @MyTable (MyVar) values (N'1234') , (N'000005') , (N'1,000') , (N'293.8457') , (N'x') , (N'+') , (N'293.8457.') , (N'......') , (N'.') ; -- This shows that Neizan's answer allows "." to slip through. select * from ( select MyVar , case when MyVar not like N'%[^0-9.]%' then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber; -- Notice the addition of "and MyVar not like '.'". select * from ( select MyVar , case when MyVar not like N'%[^0-9.]%' and MyVar not like N'%.%.%' and MyVar not like '.' then 1 else 0 end as IsNumber from @MyTable ) t order by IsNumber;