字符串处理--根据key值进行replace
1、需求:
t1表:
id
--------------------------
1,2,3,4,5
t2表:
id name
----------- ----------
1 John
2 Robot
3 Mary
期望得到结果:
1John,2Robot,3Mary,4,5
2、实现
方法一:
create table t1
(
id varchar(100)
);
insert into t1 select '1,2,3,4,5';
create table t2
(
id int,name varchar(10)
);
go
insert into t2 select 1,'John';
insert into t2 select 2,'Robot';
insert into t2 select 3,'Mary';
go
create function f_str(@id varchar(1000))
returns varchar(1000)
as
begin
set @id=','+@id+','
select @id=replace(@id,','+rtrim(id)+',',','+rtrim(id)+name+',') from t2
set @id=substring(@id,2,len(@id)-2)
return @id
end
go
select dbo.f_str(id) as name from t1;
drop function f_str;
drop table t1,t2;
(
id varchar(100)
);
insert into t1 select '1,2,3,4,5';
create table t2
(
id int,name varchar(10)
);
go
insert into t2 select 1,'John';
insert into t2 select 2,'Robot';
insert into t2 select 3,'Mary';
go
create function f_str(@id varchar(1000))
returns varchar(1000)
as
begin
set @id=','+@id+','
select @id=replace(@id,','+rtrim(id)+',',','+rtrim(id)+name+',') from t2
set @id=substring(@id,2,len(@id)-2)
return @id
end
go
select dbo.f_str(id) as name from t1;
drop function f_str;
drop table t1,t2;
方法二:
-- declaration
declare @sourceStr varchar(1000), @tempStr varchar(1000), @ret varchar(1000)
set @sourceStr = '1,3,5,10,15,20,24,25,30';
set @tempStr = ',' + @sourceStr + ',';
-- create table
create table userlist
(
rowid varchar(20), userid varchar(10), username varchar(10)
);
-- insert data
insert into userlist
select row_number() over(order by userid) as rowid, userid, username
from
(
select 3 as userid, 'Kim' as username union all
select 10,'John' union all
select 15,'Robot' union all
select 24,'Mary'
) t;
-- replace in loop
with replaced(idstring, rowid) as
(
select replace(idstring, ',' + userid + ',', ',' + userid + username + ','), rowid
from
(
select @tempStr as idstring
) a
join userlist b
on (charindex(userid, idstring) > 0) and b.rowid = 1
union all
select replace(idstring, ',' + userid + ',', ',' + userid + username + ','), b.rowid
from replaced a
join userlist b
on (charindex(userid, idstring) > 0) and (b.rowid = a.rowid + 1)
)
select @ret = substring(idstring, 2, len(idstring) - 2) from replaced;
-- show result
print(@ret);
-- clear temporary
drop table userlist;
declare @sourceStr varchar(1000), @tempStr varchar(1000), @ret varchar(1000)
set @sourceStr = '1,3,5,10,15,20,24,25,30';
set @tempStr = ',' + @sourceStr + ',';
-- create table
create table userlist
(
rowid varchar(20), userid varchar(10), username varchar(10)
);
-- insert data
insert into userlist
select row_number() over(order by userid) as rowid, userid, username
from
(
select 3 as userid, 'Kim' as username union all
select 10,'John' union all
select 15,'Robot' union all
select 24,'Mary'
) t;
-- replace in loop
with replaced(idstring, rowid) as
(
select replace(idstring, ',' + userid + ',', ',' + userid + username + ','), rowid
from
(
select @tempStr as idstring
) a
join userlist b
on (charindex(userid, idstring) > 0) and b.rowid = 1
union all
select replace(idstring, ',' + userid + ',', ',' + userid + username + ','), b.rowid
from replaced a
join userlist b
on (charindex(userid, idstring) > 0) and (b.rowid = a.rowid + 1)
)
select @ret = substring(idstring, 2, len(idstring) - 2) from replaced;
-- show result
print(@ret);
-- clear temporary
drop table userlist;