sql查询字段某个字符是否存在

wp_drifter126 2012-02-10 11:54:50
我有一个表
a b
1 1,110,20,220,200
2 10,110,2,22
2 10,110,202,20

我要查询b字段有1的所有记录
得到的数据是1 1,110,20,220,200

要查询b字段有2的所有记录
得到的数据应该是2 10,110,2,22

要查询b字段有20的所有记录
得到的数据是
1 1,110,20,220,200
2 10,110,202,20

要查询b字段有0的所有记录
得到的数据无


这样的sql怎么写????????
...全文
185 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2012-02-10
  • 打赏
  • 举报
回复

declare @t table (a int,b varchar(20))
insert into @t
select 1,'1,110,20,220,200' union all
select 2,'10,110,2,22' union all
select 2,'10,110,202,20'

declare @i int
set @i=20 --这是参数

select * from @t where charindex(','+ltrim(@i)+',',','+b+',')>0
/*
a b
----------- --------------------
1 1,110,20,220,200
2 10,110,202,20
*/
紫竹林畔 2012-02-10
  • 打赏
  • 举报
回复
select *
from tb
where charindex(',1,',','+b+',')>0 --有1的记录
select *
from tb
where charindex(',2,',','+b+',')>0 --有2的记录
select *
from tb
where charindex(',20,',','+b+',')>0 --有20的记录
一十七 2012-02-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
SQL code


declare @t table (a int,b varchar(20))
insert into @t
select 1,'1,110,20,220,200' union all
select 2,'10,110,2,22' union all
select 2,'10,110,202,20'

declare @i int
set @i=20 --……
[/Quote]

+1 正解。。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧