Beyondrelational

Saturday, March 27, 2010

Sample Merge SQL Query

if u dont mind can u me solution for 1 thing which i have prob
like for a string
ex:  this is a nice blog site for sql,


I want like @temp tables with 1 column
with rows like


this is
is a
a nice
nice blog
blog site
site for
for sql
sql


this is for 2 to words ……..is it possible


Solution:


Check if this helps…


declare @Sqlcmd1 varchar(100)
set @Sqlcmd1 = LTRIM(RTRIM(‘this is a nice blog site for sql ‘))
declare @Temp_Table table (Column1 varchar(100))


While len(@Sqlcmd1)>0
begin
if ( SELECT CHARINDEX(‘ ‘, @Sqlcmd1, CHARINDEX(‘ ‘, @Sqlcmd1)+1) ) > 0 -- checking for second space.
begin
insert into @Temp_Table (Column1) values (substring (@Sqlcmd1,1 , CHARINDEX(‘ ‘, @Sqlcmd1, CHARINDEX(‘ ‘, @Sqlcmd1)+1)))


select @Sqlcmd1 = (substring ( @Sqlcmd1
, (CHARINDEX(‘ ‘, @Sqlcmd1)+1)
, (len(@Sqlcmd1)- CHARINDEX(‘ ‘, @Sqlcmd1)+1)
)
)
End


Else
begin
insert into @Temp_Table (Column1) values (@Sqlcmd1)
Set @Sqlcmd1 =”
End


End
select * from @Temp_Table

No comments:

Post a Comment