Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何将nvarchar转换为int_Sql_Sql Server_Casting_Converters - Fatal编程技术网

Sql 如何将nvarchar转换为int

Sql 如何将nvarchar转换为int,sql,sql-server,casting,converters,Sql,Sql Server,Casting,Converters,我需要根据保存数字的nvarchar COLUMN获取数据,我得到以下错误: 将nvarchar值“362X”转换为数据类型int时,转换失败。 我试过这个: select modelcode from model where cast(modelcode as int )> 10000 - ERROR where cast(modelcode as bigint )> 10000 - ERROR where pws_modelcode+0 > 10000 - ERR

我需要根据保存数字的nvarchar COLUMN获取数据,我得到以下错误: 将nvarchar值“362X”转换为数据类型int时,转换失败。 我试过这个:

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

我缺少什么?

在SQL Server中,您可以使用
try\u convert()
try\u cast()

在其他数据库中,您可以将
大小写
与正则表达式一起使用:

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

在SQL Server中,您可以使用
try\u convert()
try\u cast()

在其他数据库中,您可以将
大小写
与正则表达式一起使用:

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 

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

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

也许因为你的值362X不是一个int值,它里面有字母?如果您想将其作为一个整数,您需要先替换X。用您正在使用的数据库标记您的问题。@SqlKindaGuy我在这列中没有X…错误表明不是这样…可能是因为您的值362X不是一个整数,它里面有字母?如果您想将其作为INT,则需要先替换X。用您正在使用的数据库标记您的问题。@SqlKindaGuy我在本列中没有X…错误说明不是这样。。。