Welcome guys..

This is programmer sharing his collection with all you ppl out there. This blog contains complete codes of java , c , c++ , unix , java script , applets , swing for learning purpose only. I try to add approx 10 new complete codes daily. Lets share out knowledge and materials in here. All your comments and votes are most welcomed.

Get your codes from..

Saturday, May 10, 2008

GENERIC LOGIC TO SEARCH & REPLACE CERTAIN CHARACTERS IN A STRING

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

No comments:

Project Source Codes