GENERIC LOGIC TO SEARCH & REPLACE CERTAIN CHARACTERS IN A STRING
create table #replchars (
lookfor char( 1 ) ,
replacewith char( 1 ) default ( '_' )
)
go
insert into #replchars values( '/' , default );
insert into #replchars values( '*' , default );
insert into #replchars values( '?' , default );
go
/* sql6x version using the stuff function */
declare @str varchar(30 )
select @str = 'abc.07/07/2000*x?'
while( exists( select * from #replchars
where charindex( lookfor , @str ) > 0 ) )
select @str = stuff( @str , charindex( lookfor , @str ) , 1 , replacewith )
from #replchars
where charindex( lookfor , @str ) > 0
print @str
/* sql70 version queries using the new replace function */
declare @str varchar(30 )
select @str = 'abc.07/07/2000*x?'
while( exists( select * from #replchars
where charindex( lookfor , @str ) > 0 ) )
select @str = replace( @str , lookfor , replacewith )
from #replchars
where charindex( lookfor , @str ) > 0
print @str
-- or the t-sql update extension
select @str = 'abc.07/07/2000*x?'
update #replchars
set @str = replace( @str , lookfor , replacewith )
where charindex( lookfor , @str ) > 0
print @str
drop table #replchars
Get your codes from..
Saturday, May 10, 2008
GENERIC LOGIC TO SEARCH & REPLACE CERTAIN CHARACTERS IN A STRING
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment