数据处理的问题

sy_binbin 2009-02-17 01:32:33
现在一个表里有N多条数据,例如

id content
1 1,2,3,4,6,9,10,11
2 1,3,7,9,11,19
3, 1,2,4,8,20


我要求把含有11的数据都给去掉,最后变成

id content
1 1,2,3,4,6,9,10
2 1,3,7,9,19
3, 1,2,4,8,20


这样用SQL语句怎么写??
...全文
96 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
pl_mm 2009-02-17
  • 打赏
  • 举报
回复
楼主,我可是写了十多分钟呢,一分也不给我....
pl_mm 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 perfectaction 的回复:]
引用 8 楼 wzy_love_sly 的回复:
6楼写的不错,支持一下


bs自己赞自己
[/Quote]
大爷的,俺先升个星也非强
nzperfect 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wzy_love_sly 的回复:]
6楼写的不错,支持一下
[/Quote]

bs自己赞自己
sy_binbin 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 pl_mm 的回复:]
bs楼主
[/Quote]

我晕了!又BS我!!!
pl_mm 2009-02-17
  • 打赏
  • 举报
回复
bs楼主
wzy_love_sly 2009-02-17
  • 打赏
  • 举报
回复
6楼写的不错,支持一下
sy_binbin 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 perfectaction 的回复:]
SQL code--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
go
create table #T (id varchar(2),content varchar(27))
insert into #T
select '1','1,2,3,4,6,9,10,11' union all
select '2','1,3,7,9,11,19' union all
select '3,','1,2,4,8,20' union all
select '4,','1,2,4,8,11,21,11,20'
update #T set content = stuff(reverse(stuff(reverse(replace(','+content+',',',11,',',')),1,…
[/Quote]

恩,这个是正确的

pl_mm 2009-02-17
  • 打赏
  • 举报
回复
create table tb(id int,content varchar(50))
insert into tb select 1,'1,2,3,4,6,9,10,11'
insert into tb select 2,'1,3,7,9,11,19'
insert into tb select 3,'11,1,2,4,8,20'
go
update a set a.content=b.content
from tb a inner join (
select id,content=substring(replace(','+content+',',',11,',','),2,len(replace(','+content+',',',11,',','))-2)
from tb
where charindex(',11,',','+content+',')>0)b on a.id=b.id

select * from tb


id content
1 1,2,3,4,6,9,10
2 1,3,7,9,19
3 1,2,4,8,20
nzperfect 2009-02-17
  • 打赏
  • 举报
回复
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
go
create table #T (id varchar(2),content varchar(27))
insert into #T
select '1','1,2,3,4,6,9,10,11' union all
select '2','1,3,7,9,11,19' union all
select '3,','1,2,4,8,20' union all
select '4,','1,2,4,8,11,21,11,20'
update #T set content = stuff(reverse(stuff(reverse(replace(','+content+',',',11,',',')),1,1,'')),1,1,'')
select * from #T

/*
id content
1 1,2,3,4,6,9,10
2 1,3,7,9,19
3, 1,2,4,8,20
4, 1,2,4,8,21,20
*/
pl_mm 2009-02-17
  • 打赏
  • 举报
回复

--这语句得保证你前面和后面,都没有[,]号,这样才能完全正确

create table tb(id int,content varchar(50))
insert into tb select 1,'1,2,3,4,6,9,10,11'
insert into tb select 2,'1,3,7,9,11,19'
insert into tb select 3,'11,1,2,4,8,20'
go

select substring(replace(','+content+',',',11,',','),2,len(replace(','+content+',',',11,',','))-2)
from tb
where charindex(',11,',','+content+',')>0
sy_binbin 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 perfectaction 的回复:]
。。。
[/Quote]

其实你的语句不能实现我想要的结果,你是在查询的时候把不想要的数据给替换掉了,但表里的数据还是没变的。

pl_mm 2009-02-17
  • 打赏
  • 举报
回复
create table tb(id int,content varchar(50))
insert into tb select 1,'1,2,3,4,6,9,10,11'
insert into tb select 2,'1,3,7,9,11,19'
insert into tb select 3,'1,2,4,8,20'
go

with cte as
(
select a.id,b.col
from (select id,cast('<items><item>'+replace(content,',','</item><item>')+'</item></items>' as xml) as col from tb where charindex(',11,',','+content+',')>0)a
cross apply
(select t.col.value('text()[1]','varchar(50)') as col from a.col.nodes('//item') as t(col))b

)
update a set a.content=b.content
from tb a inner join (
select distinct a.id,stuff(b.col.value('/root[1]','varchar(100)'),1,1,'') as content
from cte a
cross apply
(select col=(select ','+ col from cte where id=a.id and col<>11 for xml path(''),root('root'),type)) b
)b on a.id=b.id

select * from tb


id content
1 1,2,3,4,6,9,10
2 1,3,7,9,19
3 1,2,4,8,20
nzperfect 2009-02-17
  • 打赏
  • 举报
回复
。。。

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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