急求sql语句!!!!!!!!!!

qiudong_5210 2011-01-17 05:35:35

create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,' union
select '东风,卡车' union
select '商用车,卡车,' union
select '卡车,'
select * from AC_Key


我想替换掉keyWords 中的"卡车,"
替换后的结果如下
Id KeyWords
1 东风,
2
3 联合卡车,
4 商用车,

我需要的是sql语句谢谢,不要存储过程和包含cross apply的sql语句

我这里面的用","分隔开的是一个个独立的词,我所要替换的内容是整个独立的词,比如我要是替换"联合卡车,"那么就把联合卡车这个词替换掉,之所以价格","号是因为我在用这个数据的时候是用","分隔出来的
...全文
178 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
貌似不可以
????Hello1 2011-01-18
  • 打赏
  • 举报
回复
update AC_Key set KeyWords=replace(replace(keywords,'卡车',''),',','')


select * from AC_Key
这个也可以
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
#tmp是什么啊?
那个"卡车"是传进去的,也可能是联合卡车,商用车等等
liupeng_mail 2011-01-18
  • 打赏
  • 举报
回复
select id, case 
when charindex(',卡车',keywords)>1 then replace(keywords,',卡车',',')
else keywords end as keywords
into #tmp
from ac_key

select id, case
when charindex('卡车,',keywords)=1 then substring(keywords,4,len(keywords)-3)
else keywords end
from #tmp

就这个啊,你试试基本上都可以处理了
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 liupeng_mail 的回复:]
SQL code
create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,' union
select '东风,卡车' union
select '商用车,卡车,' union
sele……
[/Quote]
谁能把这个问题也给解决啊,解决了就给加分了,两种情况,一种是最后带","号的一种是最后不带","号 的
liupeng_mail 2011-01-18
  • 打赏
  • 举报
回复
create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,' union
select '东风,卡车' union
select '商用车,卡车,' union
select '卡车,'union
select '卡车,卡车' union
select '卡车,联合卡车,卡车'

update AC_Key
set KeyWords=stuff(REPLACE(','+KeyWords,',卡车,',','),1,1,'')

select * from AC_Key

/*1 东风,卡车
2
3 卡车
4 联合卡车,卡车
5 联合卡车,
6 商用车,*/

这个情况的数据,这个句子就不行了啊!!!!!!
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
解决了

update AC_Key set KeyWords=stuff(REPLACE(','+KeyWords,',东风,',','),1,1,'')

谢谢各位,谢谢飘零一叶
liupeng_mail 2011-01-18
  • 打赏
  • 举报
回复

select id, case
when charindex(',卡车',keywords)>1 then replace(keywords,',卡车',',')
else keywords end as keywords
into #tmp
from ac_key

select id, case
when charindex('卡车,',keywords)=1 then substring(keywords,4,len(keywords)-3)
else keywords end
from #tmp
飘零一叶 2011-01-18
  • 打赏
  • 举报
回复
drop table Ac_Key
create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,商用车,' union
select '东风,卡车,' union
select '商用车,卡车,' union
select '卡车,'

update AC_Key
set KeyWords=stuff(REPLACE(','+KeyWords,',卡车,',','),1,1,'')

select * from AC_Key
/*
Id KeyWords
1 东风,
2
3 联合卡车,商用车,
4 商用车,
*/
飘零一叶 2011-01-18
  • 打赏
  • 举报
回复
drop table Ac_Key
create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,商用车' union
select '东风,卡车,' union
select '商用车,卡车,' union
select '卡车,'

update AC_Key
set KeyWords=stuff(REPLACE(','+KeyWords,',卡车,',','),1,1,'')

select * from AC_Key
/*
Id KeyWords
1 东风,
2
3 联合卡车,商用车
4 商用车,
*/
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
急求结果啊
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复

drop table Ac_Key
create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,商用车' union
select '东风,卡车,' union
select '商用车,卡车,' union
select '卡车,'



出来的效果应该是这样的
1 东风,
2 NULL
3 联合卡车,商用车,
4 商用车,

而现在的效果是这样的
1 东风
2 NULL
3 联合卡车商用车
4 商用车
AcHerat 2011-01-18
  • 打赏
  • 举报
回复

create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,' union
select '东风,卡车,' union
select '商用车,卡车,' union
select '卡车,'
go


update AC_Key set KeyWords=stuff(REPLACE(','+KeyWords,',卡车,',''),1,1,'')

select * from AC_Key

drop table AC_Key

Id KeyWords
----------- ----------------------------------------------------------------------------------------------------
1 东风
2 NULL
3 联合卡车
4 商用车

(4 行受影响)
王向飞 2011-01-18
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 qiudong_5210 的回复:]

","不能出现在第一位,谢谢飘零一叶,再帮忙改一下吧
[/Quote]你再操作一下就可以啦

select SUBSTRING(',东风',2,LEN(',东风')-1)
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
","不能出现在第一位,谢谢飘零一叶,再帮忙改一下吧
飘零一叶 2011-01-18
  • 打赏
  • 举报
回复
create table AC_Key
(
Id int primary key identity(1,1),
KeyWords varchar(100)
)

insert into AC_Key
select '联合卡车,卡车,' union
select '东风,卡车,' union
select '商用车,卡车,' union
select '卡车,'
select * from AC_Key


update AC_Key set KeyWords=REPLACE(','+KeyWords,',卡车,','')

select * from AC_Key
/*
Id KeyWords
1 ,东风
2
3 ,联合卡车
4 ,商用车
*/
qiudong_5210 2011-01-18
  • 打赏
  • 举报
回复
谁能帮帮我啊
yjwcwrkks 2011-01-18
  • 打赏
  • 举报
回复
update AC_Key set KeyWords=REPLACE(','+KeyWords,',卡车,','')
liupeng_mail 2011-01-18
  • 打赏
  • 举报
回复
exec cx '卡车'

/*1 东风
2 东风,
3
4
5 联合卡车
6 联合卡车,
7 联合卡车,商用车
8 商用车,*/
liupeng_mail 2011-01-18
  • 打赏
  • 举报
回复

CREATE procedure CX(@s varchar(20))
as

select id, keywords as y,case
when charindex(','+@s+',',keywords)>1 then replace(keywords,','+@s+',',',')
else keywords end as keywords
into #tmp
from ac_key

--select * from #tmp

select id, keywords as y,case
when charindex(','+@s,keywords)>1 then replace(keywords,','+@s,'')
else keywords end as keywords
into #tmp1
from #tmp

--select * from #tmp1

select id,y, case
when charindex(@s,keywords)=1 then substring(keywords,len(@s)+1,len(keywords)-len(@s))
else keywords end as keywords
into #tmp2
from #tmp1

select id,y, case
when charindex(',',keywords)=1 then right(keywords,len(keywords)-1)
else keywords end
from #tmp2
加载更多回复(8)

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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