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

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 10:45   11   0

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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP