简体   繁体   English

如何将 nvarchar 转换为 int

[英]how to Convert nvarchar to int

I need to get data according to nvarchar coloumn that holds numbers,I get this error: Conversion failed when converting the nvarchar value '362X' to data type int.我需要根据包含数字的 nvarchar 列获取数据,我收到此错误:将 nvarchar 值“362X”转换为数据类型 int 时转换失败。 I tried this:我试过这个:

select  modelcode from model 
where  cast(modelcode as int )> 10000 - ERROR
where  cast(modelcode as bigint )> 10000 - ERROR
where  pws_modelcode+0 > 10000 - ERROR
order by ModelCode asc

What am I missing ?我错过了什么?

In SQL Server, you can use try_convert() or try_cast() :在 SQL Server 中,您可以使用try_convert()try_cast()

where try_cast(modelcode as int) > 10000

In other databases, you can use a case perhaps with regular expressions:在其他数据库中,您可以使用带有正则表达式的case

where (case when modelcode ~ '^[0-9]+$' then cast(modelcode as int) end) > 10000

将除减号以外的所有非数字字符替换为 null,然后转换为 INT。

select  modelcode from model where  cast(REGEXP_REPLACE(modelcode,'^0-9-','') as int )> 10000 

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

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