有没有一种简单的方法可以确定 varchar 是否为数字?
例子:
abc123 --> 没有数字
123 --> 是的,它是一个数字
谢谢 :)
有没有一种简单的方法可以确定 varchar 是否为数字?
例子:
abc123 --> 没有数字
123 --> 是的,它是一个数字
谢谢 :)
ISNUMERIC 不会 - 它告诉您字符串可以转换为任何数字类型,这几乎总是无意义的信息。例如,根据 ISNUMERIC,以下所有内容都是数字:
£, $, 0d0
如果您想检查数字并且只检查数字,那么您想要一个否定的 LIKE 表达式:
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.]%'
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;
不要忘记从您的数据中排除回车!!!
如:
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
如果要对具有固定长度的字段正整数添加约束
ALTER TABLE dbo.BankBranchType
ADD CONSTRAINT CK_TransitNumberMustBe5Digits
CHECK (TransitNumber NOT like '%[^0-9]%'
AND LEN(TransitNumber) = 5)