获取仅包含主题列中数字的数据集

问题描述 投票:0回答:3

我们需要从数据库中获取仅包含Subject'12345678'之类的'1234567'列中的数字的所有记录。

现在这是我尝试过的。

Select * From dbo.Lead as L Where L.Subject LIKE '#######'`

我希望此查询的结果是我想要的,但不幸的是我没有得到任何结果。

我们的SQL Server不支持ISNUMBERIC()功能。

sql sql-server sql-server-2014
3个回答
1
投票

您可以尝试使用此语句。它为您提供包含数字(包括小数)的每个条目。

SELECT * FROM TableName WHERE ColName NOT LIKE '%[^0-9.]%'

1
投票

[isnumeric()(不是isnumBeric())在SQL Server 2014中可用,并且应适合您的用例:

select * from dbo.Lead where isnumeric(subject) = 1

您也可以使用try_cast(),自SQL Server 2012起可用。

select * from dbo.Lead where try_cast(subject as int) is not null

对于不是整数(即数字序列)的subject的值,try_cast()返回null,您可以过滤掉它。

Demo on SQL Server 2014 Fiddle

with Lead as (
    select 1 id, 'abc' subject 
    union all select 2, '12345678' 
    union all select 3, '1abc'
)
select * from Lead where try_cast(subject as int) is not null;
id |学科-:| :-------2 | 12345678

0
投票

对于不超过38位的长度,您可以使用:

where try_convert(decimal(38, 0), subject) is not null

对于任意长度,您可以使用:

where subject not like '%[^0-9]%'

即,没有非数字字符。

© www.soinside.com 2019 - 2024. All rights reserved.