SQLserver在将 varchar 值 '1,2,3,4' 转换成数据类型 int 时失败

sql中使用in查询int类型拼接起来的字符串时会出现如题错误。 
原语句:

declare @ids varchar(1000) 

set @ids='1,2,3,4'                                                         

select * from users where id in (@ids)  


这样最后输出的语句其实如下:

select * from users where id in ('1,2,3,4')  

可以使用charindex结合ltrim解决该问题,如下:

declare @ids varchar(1000)

set @ids='1,2,3,4'

select * from users where charindex(','+ltrim(id)+',',','+@ids+',')>0
--------------------- 
作者:入秋枫叶 
来源:CSDN 
转载原文:https://blog.csdn.net/yiyelanxin/article/details/80095947 
 

猜你喜欢

转载自blog.csdn.net/qq_33288243/article/details/86590819