日期:2014-05-18  浏览次数:20392 次

字符串转INT型
declare @str varchar(200)
set @str = '3,314,311,312,313,316,325,2223,2224,2225,327,319,321,2219,2221,2226'
select 2544, lxbh,20120327 from t_table where lxbh in (@thlxs)

因为t_table 中的lxbh是int型,所以语句报错。现在怎么样将@str的值转换成int型

------解决方案--------------------
declare @str varchar(200)
set @str = '3,314,311,312,313,316,325,2223,2224,2225,327,319,321,2219,2221,2226'
exec('select 2544, lxbh,20120327 from t_table where lxbh in ('''+@str+''')')[code=SQL][/code]
------解决方案--------------------
探讨
declare @str varchar(200)
set @str = '3,314,311,312,313,316,325,2223,2224,2225,327,319,321,2219,2221,2226'
select 2544, lxbh,20120327 from t_table where lxbh in (@thlxs)

因为t_table 中的lxbh是int型,所以……

------解决方案--------------------
declare @str varchar(200)
set @str = '3,314,311,312,313,316,325,2223,2224,2225,327,319,321,2219,2221,2226'
select 2544, lxbh,20120327 from t_table where ','+@str+',' like '%,'+ltrim(lxbh)+',%'

------解决方案--------------------
用动态或用patindex/charindex/like
SQL code

declare @str varchar(200)
set @str = '3,314,311,312,313,316,325,2223,2224,2225,327,319,321,2219,2221,2226'
select 2544, lxbh,20120327 from t_table where charindex('%,'+rtrim(lxbh)+',%',','+@thlxs+',')>0