简体   繁体   English

如何从SQL Server的nvarchar列中选择数值?

[英]How to select numeric value from nvarchar column in sql server?

I have table like 我有像桌子

Create table Producttbl ( sno nvarchar(100),sname nvarchar(200),price nvarchar(100))

Values are 值是

sno    sname price
1       aaa   1.50
2       ccc   5.30 
abc     xxx   abc
3       dsd   kkk 
nn      dss   5.1 

Price column is nvarchar it accept all kind of data like string or numeric. Price列是nvarchar它接受所有类型的数据,例如字符串或数字。

From that table, I want to select results like this: 我要从该表中选择如下结果:

   sno        sname  price
    1           aaa   1.50
    2           ccc   5.30

Please help me. 请帮我。

You presumably want sno values which consist of digits and only digits (otherwise, you need to specify which "numeric" types you wish to accept): 您大概想要由数字和仅数字组成的sno值(否则,您需要指定要接受的“数字”类型):

select * from ProductTbl where sno not like '%[^0-9]%'

We use a double negative check, to exclude sno values which contain a non-digit character. 我们使用双重否定检查,以排除包含非数字字符的sno值。

Have you tried using the isnumeric function? 您是否尝试过使用等数字函数?

SELECT sno,sname
FROM ProductTbl
WHERE ISNUMERIC(sno)=1

You should read up on the isnumeric documentation to be sure that it returns the results you expect. 您应该阅读有关等数字的文档,以确保它返回您期望的结果。 (see comments below). (请参阅下面的评论)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM