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


nice to know, this can also be done using CTE:
http://sqlwithmanoj.wordpress.com/2011/08/18/reverse-a-string-without-using-tsqls-reverse-function/
LikeLike
thanks vishal for your comment.. i will follow that method too.
LikeLike