仅选择没有完全数值的ID

时间:2017-07-12 14:15:36

标签: sql

我想只选择某个列中没有数字值的ID。 例如:

我想选择:

a12345

但不是:

123456

这是一个nvarchar(15)字段。

这在SQL中是否可行?

迎接

3 个答案:

答案 0 :(得分:1)

尝试' isnumeric' (假设MSSQL);

select * from MyTable where isnumeric(MyField)=0

答案 1 :(得分:0)

对于第一个角色的具体问题,您可以这样做:

where substr(col, 1, 1) not between '0' and '9'

这应该适用于几乎所有数据库(尽管substr()可能拼写为substring())。

有更好的解决方案,但它们特定于给定的数据库。

答案 2 :(得分:0)

如果是SQL Server 2012+,我会使用try_convert()为任何转换错误返回NULL

示例

Declare @YourTable Table ([ID] nvarchar(15))
Insert Into @YourTable Values 
 ('a12345')
,('12345')
,('123-45')
,('12345z')

Select * from @YourTable
 where try_convert(int,ID) is null

<强>返回

ID
a12345
123-45
12345z