Reverse of a string with out using Reverse function


Hi All,

Below is the script to find the reverse of the given string without using built in function Reverse

To display the reverse of the string like a scalar variable

use tempdb

go

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

begin

drop table #temp

end

go

declare @test varchar(100)

set @test=’chaitanya’

declare @count int

select @count=len(@test)

declare @i int

set @i=1

create table #temp

(

column1 varchar(100)

)

declare @result varchar(100)

set @result=”

while(@i <=@count)

begin

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

set @i=@i+1

end

insert #temp(column1)

select @result

select * from #temp

Result:

If you want to display the string with incremental values and it should be in reverse order

use tempdb

go

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

begin

drop table #temp

end

go

declare @test varchar(100)

set @test=’chaitanya’

declare @count int

select @count=len(@test)

declare @i int

set @i=1

create table #temp

(

column1 varchar(100)

)

declare @result varchar(100)

set @result=”

while(@i <=@count)

begin

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

set @i=@i+1

insert #temp(column1)

select @result

end

select * from #temp

Result:

Please check and let us know if you have any doubts on this.

Thanks for viewing

Regards,

Chaitanya

Visit site: https://mssqlbuzz.wordpress.com

Send an Email : mssqlbuzz@gmail.com

2 responses

    1. Chaitanya Talasila's avatar

      thanks vishal for your comment.. i will follow that method too.

      Like

Leave a reply to Vishal Cancel reply

Design a site like this with WordPress.com
Get started