34,873
社区成员
发帖
与我相关
我的任务
分享--text字段替换处理示例
--邹建 2004.07(引用请保留此信息)
--测试数据
create table tb(id int identity(1,1),content ntext)
insert tb select '001,002 '
union all select '001,002,003,004,005,006,007,008,009,010 '
go
--替换处理
declare @s_str nvarchar(4000),@r_str nvarchar(4000)
select @s_str= '02 ' --要替换的字符串
,@r_str= '**02 ' --替换成该字符串
--替换处理
declare @id int,@ptr varbinary(16)
declare @start int,@s nvarchar(4000),@len int
declare @s_str1 nvarchar(4000),@s_len int,@i int,@step int
select @s_str1=reverse(@s_str),@s_len=len(@s_str)
,@step=case when len(@r_str)> len(@s_str)
then 4000/len(@r_str)*len(@s_str)
else 4000 end
declare tb cursor local for
select id,start=charindex(@s_str,[content])-1
from [tb]
where id=2 --替换id=1的记录
and charindex(@s_str,[content])> 0
open tb
fetch tb into @id,@start
while @@fetch_status=0
begin
select @ptr=textptr([content])
,@s=substring([content],@start+1,@step)
from [tb]
where id=@id
while len(@s)> =@s_len
begin
select @len=len(@s),@i=charindex(@s_str1,reverse(@s))
if @i> 0
begin
select @i=case when @i> =@s_len then @s_len else @i end
,@s=replace(@s,@s_str,@r_str)
updatetext [tb].[content] @ptr @start @len @s
end
else
set @i=@s_len
select @start=@start+len(@s)-@i+1
,@s=substring([content],@start+1,@step)
from [tb]
where id=@id
end
fetch tb into @id,@start
end
close tb
deallocate tb
go
--显示处理结果
select * from tb
go
--删除测试
drop table tb
/*--测试结果
id content
----------- ----------------------------------------------
1 001,002
2 001,0**02,003,004,005,006,007,008,009,010
(所影响的行数为 2 行)
--*/
CREATE TABLE TB (aa NVARCHAR(4000))
INSERT INTO tb
SELECT '1xxxxAttractionsxxxxxxxxx1
Attractions
2xxxxxxxxxAttractionsxxxxxxxxx2
Shopping
3xxxxxxxxxxxxxxxxxxxxxxxxxxx3
Eating Out
4xxxxxxxxxxxxxxxxxxxxx4
Accommodation
5xxxxxxxxxxxxxAccommodationxxxxxxxxxxxxx5
Transportation
6xxxxxxxxxxx6
Climate
7xxxxxxxxxxxxxxxxxxxxxxxxxxxx7
'
CREATE TABLE TB (aa NVARCHAR(4000))
INSERT INTO tb
SELECT '1xxxxxxxxxxxxxxxxxxxx1
Attractions
2xxxxxxxxxAttractionsxxxxxxxxx2
Shopping
3xxxxxxxxxxxxxxxxxxxxxxxxxxx3
Eating Out
4xxxxxxxxxxxxxxxxxxxxx4
Accommodation
5xxxxxxxxxxxxxAccommodationxxxxxxxxxxxxx5
Transportation
6xxxxxxxxxxx6
Climate
7xxxxxxxxxxxxxxxxxxxxxxxxxxxx7
'
UPDATE tb
SET aa=REPLACE(aa,'Attractions
','''as brief,''')
UPDATE tb
SET aa=REPLACE(aa,'Shopping
','''as Attractions,''')
UPDATE tb
SET aa=REPLACE(aa,'Eating Out
','''as Shopping,''')
UPDATE tb
SET aa=REPLACE(aa,'Accommodation
','''as [ating Out],''')
UPDATE tb
SET aa=REPLACE(aa,'Transportation
','''as Accommodation,''')
UPDATE tb
SET aa=REPLACE(aa,'Climate
','''as Transportation,''')
DECLARE @sql NVARCHAR(4000)
SELECT @sql='select '''+aa+''' as Climate FROM tb ' FROM dbo.TB
EXEC(@sql)
--------------------------结果
brief Attractions Shopping ating Out Accommodation Transportation Climate
1xxxxxxxxxxxxxxxxxxxx1 2xxxxxxxxxAttractionsxxxxxxxxx2 3xxxxxxxxxxxxxxxxxxxxxxxxxxx3 4xxxxxxxxxxxxxxxxxxxxx4 5xxxxxxxxxxxxxAccommodationxxxxxxxxxxxxx5 6xxxxxxxxxxx6 7xxxxxxxxxxxxxxxxxxxxxxxxxxxx7

CREATE TABLE TB (aa NVARCHAR(4000))
INSERT INTO tb
SELECT '1xxxxxxxxxxxxxxxxxxxx1
Attractions
2xxxxxxxxxxxxxxxxxxxx2
Shopping
3xxxxxxxxxxxxxxxxxxxxxxxxxxx3
Eating Out
4xxxxxxxxxxxxxxxxxxxxx4
Accommodation
5xxxxxxxxxxxxxxxxxxxxxxxxxxx5
Transportation
6xxxxxxxxxxx6
Climate
7xxxxxxxxxxxxxxxxxxxxxxxxxxxx7
'
UPDATE tb
SET aa=REPLACE(aa,'Attractions','''as brief,''')
UPDATE tb
SET aa=REPLACE(aa,'Shopping','''as Attractions,''')
UPDATE tb
SET aa=REPLACE(aa,'Eating Out','''as Shopping,''')
UPDATE tb
SET aa=REPLACE(aa,'Accommodation','''as [ating Out],''')
UPDATE tb
SET aa=REPLACE(aa,'Transportation','''as Accommodation,''')
UPDATE tb
SET aa=REPLACE(aa,'Climate','''as Transportation,''')
DECLARE @sql NVARCHAR(4000)
SELECT @sql='select '''+aa+''' as Climate FROM tb ' FROM dbo.TB
EXEC(@sql)
DECLARE @STR NVARCHAR(MAX)
SET @STR='1xxxxxxxxxxxxxxxxxxxx1
Attractions
2xxxxxxxxxxxxxxxxxxxx2
Shopping
3xxxxxxxxxxxxxxxxxxxxxxxxxxx3
Eating Out
4xxxxxxxxxxxxxxxxxxxxx4
Accommodation
5xxxxxxxxxxxxxxxxxxxxxxxxxxx5
Transportation
6xxxxxxxxxxx6
Climate
7xxxxxxxxxxxxxxxxxxxxxxxxxxxx7
'
SELECT LEFT(@STR,CHARINDEX('Attractions',@STR)-1) AS Brief
,SUBSTRING(@STR,CHARINDEX('Attractions',@STR)+11,CHARINDEX('Shopping',@STR)-CHARINDEX('Attractions',@STR)-11) AS Attractions
,SUBSTRING(@STR,CHARINDEX('Shopping',@STR)+8,CHARINDEX('Eating Out',@STR)-CHARINDEX('Shopping',@STR)-8) AS Shopping
,SUBSTRING(@STR,CHARINDEX('Eating Out',@STR)+10,CHARINDEX('Accommodation',@STR)-CHARINDEX('Eating Out',@STR)-10) AS [Eating Out]
,SUBSTRING(@STR,CHARINDEX('Accommodation',@STR)+13,CHARINDEX('Transportation',@STR)-CHARINDEX('Accommodation',@STR)-13) AS Accommodation
,SUBSTRING(@STR,CHARINDEX('Transportation',@STR)+14,CHARINDEX('Climate',@STR)-CHARINDEX('Transportation',@STR)-14) AS Transportation
,RIGHT(@STR,LEN(@STR)-CHARINDEX('Climate',@STR)-7) AS Climate
----------------------------
Brief Attractions Shopping Eating Out Accommodation Transportation Climate
1xxxxxxxxxxxxxxxxxxxx1 2xxxxxxxxxxxxxxxxxxxx2 3xxxxxxxxxxxxxxxxxxxxxxxxxxx3 4xxxxxxxxxxxxxxxxxxxxx4 5xxxxxxxxxxxxxxxxxxxxxxxxxxx5 6xxxxxxxxxxx6 7xxxxxxxxxxxxxxxxxxxxxxxxxxxx7
