是否有一种简单的方法可以确定varchar是否为数字?
示例:
abc123 - >没有号码
123 - >是的,它是一个数字
谢谢:)
答案 0 :(得分:126)
ISNUMERIC不会这样做 - 它告诉你字符串可以转换为任何的数字类型,这几乎总是一条无知的信息。例如,根据ISNUMERIC:
,以下所有内容均为数字£,$,0d0
如果要检查数字和仅数字,可以使用负LIKE表达式:
not Value like '%[^0-9]%'
答案 1 :(得分:29)
在文章中也查看NOTES部分。
答案 2 :(得分:25)
你可以这样检查
declare @vchar varchar(50)
set @vchar ='34343';
select case when @vchar not like '%[^0-9]%' then 'Number' else 'Not a Number' end
答案 3 :(得分:17)
使用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
答案 4 :(得分:8)
我遇到了允许小数值的需要,因此我使用了not Value like '%[^0-9.]%'
答案 5 :(得分:3)
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;
答案 6 :(得分:1)
DECLARE @A nvarchar(100) = '12'
IF(ISNUMERIC(@A) = 1)
BEGIN
PRINT 'YES NUMERIC'
END
答案 7 :(得分:1)
Neizan的代码只允许a"。"通过。由于过于迂腐的风险,我又添加了一个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;
答案 8 :(得分:1)
Damien_The_Unbeliever指出他只对数字有好处
Wade73增加了一点来处理小数点
neizan做了一个额外的调整,就像notwhereuareat
一样不幸的是,似乎都没有处理负值,并且它们似乎在值中有逗号问题...
这是我调整负值和带逗号
的调整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;
答案 9 :(得分:0)
别忘了从数据中排除回车!!!
如:
SELECT
Myotherval
, CASE WHEN TRIM(REPLACE([MyVal], char(13) + char(10), '')) not like '%[^0-9]%' and RTRIM(REPLACE([MyVal], char(13) + char(10), '')) not like '.' and isnumeric(REPLACE([MyVal], char(13) + char(10), '')) = 1 THEN 'my number: ' + [MyVal]
ELSE ISNULL(Cast([MyVal] AS VARCHAR(8000)), '')
END AS 'MyVal'
FROM MyTable