简体   繁体   中英

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. 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() :

where try_cast(modelcode as int) > 10000

In other databases, you can use a case perhaps with regular expressions:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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