当前位置: 代码迷 >> Sql Server >> 这样的程序该怎样写,该如何解决
  详细解决方案

这样的程序该怎样写,该如何解决

热度:29   发布时间:2016-04-27 16:18:07.0
这样的程序该怎样写
表一中有两个字段field1,field2
[email protected]@[email protected]@000107
[email protected]@[email protected]

[email protected]?换。
替换规则:
[email protected]ield1中删除,比如说000101在两者中都出现过,则从field1中删除,然后进行下一次比较,如000105也在二者中出现,则将其从field1中删除掉,[email protected]@[email protected]

------解决方案--------------------
create table #t(field1 varchar(100),field2 varchar(100))
insert into #t
select '[email protected]@[email protected]@000107 ', '[email protected]@[email protected] '
GO
create function udf_splitstring
(
@str varchar(8000) --要分拆的字符串
,@spli varchar(10) --字符串分隔符
)
returns @retab table(istr varchar(8000))
as
begin
declare @i int
declare @splen int
select @splen=len(@spli),@i=charindex(@spli,@str)
while @i > 0
begin
insert into @retab
values(left(@str,@i-1))
select @str=substring(@str,@[email protected],8000)
select @i=charindex(@spli,@str)
end
if @str <> ' ' insert into @retab values(@str)
return
end
GO
create function udf_replacestr
(@str1 varchar(100),
@str2 varchar(100))
returns varchar(100)
as
begin
select @str1=replace(@str1,t.istr+ '@ ', ' ')
from udf_splitstring(@str2, '@ ')t
where charindex(t.istr,@str1)> 0
return @str1
end
GO
select dbo.udf_replacestr(field1,field2) from #t
/*

--------------------------
[email protected]@000107

(所影响的行数为 1 行)
*/
  相关解决方案