如何删除数组中元素?

cougar 2008-04-08 12:46:46
数据表如下
ID 字段A
1 10,13,23,33
2 11,12,13
3 13,15,18

要删除所有条记录字段A中的13

得出如下结果.
ID 字段A
1 10,23,33
2 11,12
3 15,18
...全文
219 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-04-08
  • 打赏
  • 举报
回复
declare @s varchar(10)
set @s = '13'
select id,replace(字段A,','+@s+',','') as 字段A
from ta
Limpire 2008-04-08
  • 打赏
  • 举报
回复
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ID int,字段A varchar(100))
insert into #T
select 1,'10,13,23,33' union all
select 2,'11,12,13' union all
select 3,'13,15,18'

update #T set 字段A = replace(字段A, ',13,', ',') where charindex(',13,',字段A)>0
update #T set 字段A = left(字段A, len(字段A) - 3) where right(字段A, 3) = ',13'
update #T set 字段A = right(字段A, len(字段A) - 3) where left(字段A, 3) = '13,'

select * from #T

/*
ID 字段A
1 10,23,33
2 11,12
3 15,18
*/
sdyqingdao 2008-04-08
  • 打赏
  • 举报
回复
replace()
Limpire 2008-04-08
  • 打赏
  • 举报
回复
啊,老兄,我开玩笑而已,抱歉!
-狙击手- 2008-04-08
  • 打赏
  • 举报
回复
不考虑效率



create table #T (ID int,字段A varchar(100))
insert into #T
select 1,'10,13,13,13,13,23,33' union all
select 2,'11,132,13' union all
select 3,'13,15,138'

select top 2000 id=identity(int,1,1) into # from syscolumns a,syscolumns b
select a.id,
字段A=substring(a.字段A,b.id,charindex(',',a.字段A+',',b.id)-b.id)
into tTest
from #T a, # b
where substring(','+a.字段A,b.id,1)=','
order by a.id,b.id


go

create function getstr(@content varchar(100),@i int)
returns varchar(2000)
as
begin
declare @str varchar(2000)
set @str=''
select @str=@str+','+ltrim(字段A) from tTest where id=@content and 字段A <> @i
select @str=right(@str,len(@str)-1)
return @str
end
go

select id,dbo.getstr(id,13) as 字段A
from tTest
group by id

drop table #T,#,tTest
drop function getstr


/*



id 字段A
----------- -----------
1 10,23,33
2 11,132
3 15,138

(所影响的行数为 3 行)
*/
-狙击手- 2008-04-08
  • 打赏
  • 举报
回复
select 1,'10,13,13,13,13,23,33' union all
select 2,'11,132,13' union all
select 3,'13,15,138'

小楼, 这搞得我为难了

试试
Limpire 2008-04-08
  • 打赏
  • 举报
回复
哈哈,happyflystone好样的,再挑战一下:

create table #T (ID int,字段A varchar(100))
insert into #T
select 1,'10,13,13,13,13,23,33' union all
select 2,'11,132,13' union all
select 3,'13,15,138'


我选UPDATE

while exists (select 1 from #T where charindex(',13,',字段A) > 0)
update #T set 字段A = replace(字段A, ',13,', ',') where charindex(',13,', 字段A) > 0


-狙击手- 2008-04-08
  • 打赏
  • 举报
回复
create table #T (ID int,字段A varchar(100))
insert into #T
select 1,'10,13,23,33' union all
select 2,'11,132,13' union all
select 3,'13,15,138'



declare @s varchar(10)
set @s = '13'
select id,substring(reverse(stuff(reverse(replace(','+字段A+',',','+@s+',',',')),1,1,'')),2,100) as 字段A

from #T


drop table #T
/*
id 字段A
----------- -----------
1 10,23,33
2 11,12
3 15,138

(所影响的行数为 3 行)
*/
Limpire 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happyflystone 的回复:]
引用 4 楼 Limpire 的回复:
这个不能用简单的replace(),因为无法保证其它元素不包含13。

happyflystone的也不对。


已经 修正
[/Quote]

试试:

3,'13,15,18'
-->
3,'13,15,138'
-狙击手- 2008-04-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Limpire 的回复:]
这个不能用简单的replace(),因为无法保证其它元素不包含13。

happyflystone的也不对。
[/Quote]

已经 修正
-狙击手- 2008-04-08
  • 打赏
  • 举报
回复
create table #T (ID int,字段A varchar(100))
insert into #T
select 1,'10,13,23,33' union all
select 2,'11,12,13' union all
select 3,'13,15,18'



declare @s varchar(10)
set @s = '13'
select id,stuff(replace(','+字段A,','+@s,''),1,1,'') as 字段A
from #T


drop table #T
/*
id 字段A
----------- -----------
1 10,23,33
2 11,12
3 15,18

(所影响的行数为 3 行)
*/
Limpire 2008-04-08
  • 打赏
  • 举报
回复
这个不能用简单的replace(),因为无法保证其它元素不包含13。

happyflystone的也不对。

34,838

社区成员

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

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