请教如何在一个字符串有分割符的某个位置插入字符串

henjiandan 2011-08-17 12:38:03
比如:
有一条记录:111111,222222,333333,444444,555555


问题1:如何在 每个分割符的一个字符串中的中间加上“GO”

使结果变成:
111GO111,222GO222,333GO333,444GO444,555GO555

问题2:已有变量 @name=222222

如何在 当前匹配字符串的中的中间加上“GO”

使结果变成:
111111,222GO222,333333,444444,555555



因为两个问题有关联,所以开到一个帖子了,每个30分,请高人帮忙!
...全文
224 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010579852 2013-12-19
  • 打赏
  • 举报
回复
急!!!!以],[分割一条字符串,怎么分割
AcHerat 元老 2011-08-17
  • 打赏
  • 举报
回复
去看5楼的 -- 1
将ach和你的表结合起来更新下就是了,改下截取的位置。
henjiandan 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 maco_wang 的回复:]
引用 8 楼 henjiandan 的回复:

引用 4 楼 maco_wang 的回复:
SQL code

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=3333……
[/Quote]

这个解决问题了! 还有如果是update分割符中所有的第一位后加个 Go 怎么写?
叶子 2011-08-17
  • 打赏
  • 举报
回复
11#是为了避免
1GO11111,233333,33333,444444,555555
这种情况中5个3定位不准,所以显得比之前麻烦了一些。
叶子 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 henjiandan 的回复:]

引用 4 楼 maco_wang 的回复:
SQL code

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=33333 --换成33333

update @t ……
[/Quote]

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name='111111' --111111

update @t set col=
substring(replace(','+col+',',','+@name+',',
','+left(@name,1)+'GO'+right(@name,len(@name)-1)+','),2,
len(replace(','+col+',',','+@name+',',
','+left(@name,1)+'GO'+right(@name,len(@name)-1)+','))-2)
where charindex(','+@name+',',','+col+',')>0

select * from @t
/*
1GO11111,222222,33333,444444,555555
*/
chtzhking 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 henjiandan 的回复:]

引用 4 楼 maco_wang 的回复:
SQL code

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=33333 --换成33333

update @t ……
[/Quote]
叶子的语句变一下就可以了

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=555555 --换成33333

update @t set col=
replace(col,@name,
left(@name,1)+'GO'+right(@name,len(@name)-1))
where charindex(','+@name+',',','+col+',')>0

select * from @t
henjiandan 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
偶数长度加上中间,例如字符为5个,例如22222 是不是变成22GO222?
[/Quote]

可能是我举的例子不好,在第一个位置加 Go就行。 比如111111 变成 1Go11111
henjiandan 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 maco_wang 的回复:]
SQL code

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=33333 --换成33333

update @t set col=
replace(col,',……
[/Quote]


叶子这个是我想要的,但是这个有问题 第一个111111 和最后的一个字符串 555555 不起作用。

还有如果所有分隔符都加GO 该怎么做呢?
chtzhking 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 acherat 的回复:]

SQL code

create table tb(id int identity(1,1),col varchar(40))
insert into tb
select '111111,222222,33333,444444,555555'
go

declare @col varchar(20)
set @col = '222222'

-- 1
;with ach as
(
se……
[/Quote]很强大,系统表的利用好熟练啊,学习了
gogodiy 2011-08-17
  • 打赏
  • 举报
回复
问题一:如果长度一致,那么

create table t1
(
col varchar(50)
)
insert into t1
select '111111,222222,333333,444444,555555'
select * from t1

;with aaa as
(select col,number from t1 as a inner join master..spt_values as b on SUBSTRING(','+col,number,1)=','
and type='P')
,bbb as
(select SUBSTRING(col,number,CHARINDEX(',',col+',',number)-number) as col from aaa)
select LEFT(col,3)+'GO'+RIGHT(col,3) as col from bbb
AcHerat 元老 2011-08-17
  • 打赏
  • 举报
回复

create table tb(id int identity(1,1),col varchar(40))
insert into tb
select '111111,222222,33333,444444,555555'
go

declare @col varchar(20)
set @col = '222222'

-- 1
;with ach as
(
select a.id,substring(a.col,b.number,charindex(',',a.col+',',b.number)-b.number) as col
from tb a,master..spt_values b
where b.[type] = 'p' and b.number between 1 and len(a.col)
and substring(','+a.col,b.number,1) = ','
)

select id,stuff(
(select ','+left(col,len(col)/2)+'GO'+right(col,len(col)-len(col)/2)
from ach where id = t.id for xml path('')),
1,1,'') as col
from ach t
group by id

-- 2
select id,replace(col,','+@col+',',
','+left(@col,len(@col)/2)+'GO'+right(@col,len(@col)-len(@col)/2)+',') as col
from tb

drop table tb

/*****************

id col
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 111GO111,222GO222,33GO333,444GO444,555GO555

(1 行受影响)

id col
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 111111,222GO222,33333,444444,555555

(1 行受影响)
叶子 2011-08-17
  • 打赏
  • 举报
回复

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=33333 --换成33333

update @t set col=
replace(col,','+@name+',',
','+left(@name,len(@name)/2)+'GO'+right(@name,len(@name)-len(@name)/2)+',')
where charindex(','+@name+',',','+col+',')>0

select * from @t
/*
111111,222222,33GO333,444444,555555
*/
叶子 2011-08-17
  • 打赏
  • 举报
回复

declare @t table (col varchar(40))
insert into @t
select '111111,222222,33333,444444,555555'

declare @name varchar(20)
set @name=222222

update @t set col=
replace(col,','+@name+',',
','+left(@name,len(@name)/2)+'GO'+right(@name,len(@name)-len(@name)/2)+',')
where charindex(','+@name+',',','+col+',')>0

select * from @t
/*
111111,222GO222,33333,444444,555555
*/
叶子 2011-08-17
  • 打赏
  • 举报
回复
偶数长度加上中间,例如字符为5个,例如22222 是不是变成22GO222?
  • 打赏
  • 举报
回复
长度固定么?
henjiandan 2011-08-17
  • 打赏
  • 举报
回复
有人帮忙改下吗?
henjiandan 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 acherat 的回复:]
去看5楼的 -- 1
将ach和你的表结合起来更新下就是了,改下截取的位置。
[/Quote]

你这个语法 看不懂,小三~ 麻烦帮忙改下吧

34,873

社区成员

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

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