日期:2014-05-16 浏览次数:20470 次
create table test (code nvarchar(20))
insert into test
select 'abc-asd-EA-TF:ASAD'
union all
select 'bcda-esa-TF:ASDFASDF'
select case when code like '%EA-TF%' then code
when code like '%TF%' then replace(code,'TF','替换字符')
else code
end
from test
----------------------------------------------------------------
-- Author :TravyLee(走自己的路,让狗去叫吧!)
-- Date :2014-02-18 15:00:20
-- Version:
-- Microsoft SQL Server 2008 (SP2) - 10.0.4000.0 (X64)
-- Sep 16 2010 19:43:16
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb([code] varchar(50))
insert #tb
select 'abc-asd-EA-TF:ASAD' union all
select 'bcda-esa-TF:ASDFASDF'
go
update #tb
set code=replace(code,'TF','替换字符')
where CHARINDEX('TF',code)-CHARINDEX('EA-',code)<>3
select * from #tb
/*
code
---------------------------------
abc-asd-EA-TF:ASAD
bcda-esa-替换字符:ASDFASDF
*/
REPLACE(REPLACE(REPLACE(code,'EA-TF','<$$$>'),'TF','XXX'),'<$$$>','EA-TF')