increment the string value letter by letter for every row


Hi ,

This is my first post in this blog.  yesterday my friend  asked  me how to display the sting letter by letter in sql server

we have string function substring used here

below is the script for this

use tempdb

go

if object_id(‘tempdb..#temp’) is not null

begin

drop table #temp

end

go

— declaring the  variable to store the string

declare @test varchar(100)

set @test=’chaitanya’  — assigning the value to the variable

declare @count int

select @count=len(@test)

declare @i int

set @i=1

create table #temp

(

column1 varchar(100)

)

declare @result varchar(100)

declare @j int

set @j=0

while(@i <=@count)

begin

select @result =substring(@test,@j,@i+1)

insert #temp(column1)

select @result

set @i=@i+1

end

select  * from #temp

  –result

c

ch

cha

chai

chait

chaita

chaitan

chaitany

chaitanya

Please let me know if you have any concerns or any suggestions

 

Regards,

Chaitanya

Leave a comment

Design a site like this with WordPress.com
Get started