我的表中的一个字段的类型是varchar
数据是
3+102
4+293
233+3
345 我写了sql语句
select email,coin=
case when charindex('+', [state])>0
then left([state],charindex('+',[state])-1)
else [state] end
from xzdb.dbo.item where [item]='coin' order by coin这样得到的coin值是字符串类型的。
我用cast或者convert转换成int值时,提示我说
在将 varchar 值 '3 ' 转换成数据类型 int 时失败。请问是什么问题啊
数据是
3+102
4+293
233+3
345 我写了sql语句
select email,coin=
case when charindex('+', [state])>0
then left([state],charindex('+',[state])-1)
else [state] end
from xzdb.dbo.item where [item]='coin' order by coin这样得到的coin值是字符串类型的。
我用cast或者convert转换成int值时,提示我说
在将 varchar 值 '3 ' 转换成数据类型 int 时失败。请问是什么问题啊
cast(ltrim(rtrim(coin)) as int)
case when charindex('+', [state])>0
then left([state],charindex('+',[state])-1)
else [state] end
from xzdb.dbo.item where [item]='coin' order by
cast((case when charindex('+', [state])>0
then left([state],charindex('+',[state])-1)
else [state] end)as int)
declare @t table(state varchar(20))insert @t
select '3+102' union all
select '4+293' union all
select '233+3' union all
select '345'
select coin=
case when charindex('+', [state])>0
then cast(left([state],charindex('+',[state])-1) as int)
else [state] end
from @t/*--------查询结果----------
3
4
233
345
------------------*/