varchar 值 '2123,2385,4516' 转换成数据类型 int 时失败

--通常的where id in (1,2,3,4,5,6...)如单个数字可以,如下:
select * from tb_UserRelationWebsite where PublishmentSystemID in(2123,2385,2588)
--ok,能正常运行
--但是如果是一个字符串'2123,2385,2588...'
select * from tb_UserRelationWebsite where PublishmentSystemID in('2123,2385,2588')
--会报错误提示:在将 varchar 值 '2123,2385,2588' 转换成数据类型 int 时失败。
--可以用如下方法:where charindex(rtrim(字段名), '字符串如:2123,2385,2588...')>0
select * from tb_UserRelationWebsite where charindex(rtrim(PublishmentSystemID), '2123,2385,2588')>0

--嵌套子查询也是没问题的

select * from liveserver_BlogContent 
where UserName in (
                   select UserName from tb_UserRelationWebsite 
				   where 
				   charindex(rtrim(PublishmentSystemID), 
				                  (select wr.BeRelateID from tb_UserRelationWebsite uw 
						   left join tb_WebsiteRelation as wr 
						   on wr.PublishmentSystemID = uw.PublishmentSystemID 
						   where UserName = 'wang')
						   )>0)


你可能感兴趣的:(varchar 值 '2123,2385,4516' 转换成数据类型 int 时失败)