﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Programming / General  / Reverse string without built in functions / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 17:56:04 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Here's another method:[code="sql"]-- ParametersDECLARE @String VARCHAR(8000)SET @String = 'Reverse'-- Local VariablesDECLARE @OutputString VARCHAR(8000); SET @OutputString = '';DECLARE @StrLen INT; SET @StrLen = DATALENGTH(@String);DECLARE @MappingTable TABLE (n INT PRIMARY KEY CLUSTERED); INSERT INTO @MappingTable (n) 	SELECT TOP(@StrLen) n = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM syscolumns a, syscolumns b;UPDATE @MappingTable SET @OutputString = @OutputString + SUBSTRING(@String,(@StrLen+1)-n,1); SELECT FormattedAmount = @OutputString;[/code]</description><pubDate>Thu, 24 Jan 2013 08:47:57 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Just modify the next iteration call a bit, and the recursive solution works:ALTER function dbo.StringReverse( @InString varchar(20))returns varchar(20)ASbegindeclare @RevString varchar(20)IF len(@InString) in (0,1) set @RevString = @InStringELSEset @RevString = ( RIGHT(@instring,1)  -- Could use SUBSTRING, but why not RIGHT ? + dbo.StringReverse(substring(@InString, 1, len(@InString)-1)) )return @RevStringendObviously, "WITHOUT using any built-in functions" isn't quite accurate, as SUBSTRING and LEN are used, So if there are exceptions, RIGHT could also be used to provide better clarity...</description><pubDate>Thu, 24 Jan 2013 08:15:23 GMT</pubDate><dc:creator>James_R_Alves</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]abhi620 (5/20/2011)[/b][hr]i think this is good solution for...[/quote]With only very few exceptions, the RBAR of a WHILE loop is almost never a good solution.  Please try it on a couple of hundred thousand rows and see.</description><pubDate>Sat, 21 May 2011 22:06:07 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>hi,it is batch code it gives revers of all rows in the table........i think this is good solution for --reversing data with out using REVERSE() functioncreate table std(id int identity,name varchar(20))insert into std(name)values ('abhijeet'),('omkar'),('tushar')create table #table(id int ,name varchar(20))declare @str varchar(20) --if exists(select * from std)  --begin	declare @count int	set @count=(select COUNT(*) from std)		--declare @tempcntid int=@count	declare	@i int=@count			--while exists(select * from std)	while @i!=0	begin		set @str=(select name from std where id=@i)		declare	@str2 varchar(20)=' '		declare @j int=1						while @j&amp;lt;=LEN(@str)			begin				set @str2=SUBSTRING(@str,@j,1)+ @str2				set @j=@j+1						end			--print @str2			insert into #table(id,name)values(@i,@str2)			--set @tempcntid=@tempcntid-1	set @i=@i-1	end	goselect * from stdgoselect * from #tablego</description><pubDate>Fri, 20 May 2011 22:51:06 GMT</pubDate><dc:creator>abhi620</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]GSquared (4/20/2009)[/b][hr]That's not part of the addition.  That's just setting up a framework to run it.  Run the query without that, with the numbers directly in the Where clause, instead of using variables, you'll be fine.If you truly want addition without any mathematical, logical, etc., arguments, here you go:Question: "What's some number added to another number?"  (Can't have assignments in there, so you can't actually state what numbers you want to add.)Answer: "A number."It's a correct answer!  Woot! :-)[/quote]I was gonna suggest sp_Abacus, but your solution runs faster.</description><pubDate>Mon, 20 Apr 2009 08:43:32 GMT</pubDate><dc:creator>jcrawf02</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>That's not part of the addition.  That's just setting up a framework to run it.  Run the query without that, with the numbers directly in the Where clause, instead of using variables, you'll be fine.If you truly want addition without any mathematical, logical, etc., arguments, here you go:Question: "What's some number added to another number?"  (Can't have assignments in there, so you can't actually state what numbers you want to add.)Answer: "A number."It's a correct answer!  Woot! :-)</description><pubDate>Mon, 20 Apr 2009 07:58:34 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]GSquared (4/17/2009)[/b][hr][quote][b]Michael Valentine Jones (4/17/2009)[/b][hr]OK, as long as we are doing useless SQL, I want to add numbers together without using any Arithmetic, Logical, Assignment, String, Bitwise, or Unary Operators or any Mathematical or Aggregate Functions.[/quote][code]create table #Sums (Num1 int not null,Num2 int not null,Sum12 int);insert into #Sums (Num1, Num2, Sum12)select 0, 0, 0 union allselect 1, 0, 1 union allselect 2, 0, 2 union allselect 3, 0, 3 union allselect 4, 0, 4 union allselect 5, 0, 5 union allselect 6, 0, 6 union allselect 7, 0, 7 union allselect 8, 0, 8 union allselect 9, 0, 9 union allselect 0, 1, 1 union allselect 1, 1, 2 union allselect 2, 1, 3 union allselect 3, 1, 4 union allselect 4, 1, 5 union allselect 5, 1, 6 union allselect 6, 1, 7 union allselect 7, 1, 8 union allselect 8, 1, 9 union allselect 9, 1, 10 union allselect 0, 2, 2 union allselect 1, 2, 3 union allselect 2, 2, 4 union allselect 3, 2, 5 union allselect 4, 2, 6 union allselect 5, 2, 7 union allselect 6, 2, 8 union allselect 7, 2, 9 union allselect 8, 2, 10 union allselect 9, 2, 11 union allselect 0, 3, 3 union allselect 1, 3, 4 union allselect 2, 3, 5 union allselect 3, 3, 6 union allselect 4, 3, 7 union allselect 5, 3, 8 union allselect 6, 3, 9 union allselect 7, 3, 10 union allselect 8, 3, 11 union allselect 9, 3, 12 union allselect 0, 4, 4 union allselect 1, 4, 5 union allselect 2, 4, 6 union allselect 3, 4, 7 union allselect 4, 4, 8 union allselect 5, 4, 9 union allselect 6, 4, 10 union allselect 7, 4, 11 union allselect 8, 4, 12 union allselect 9, 4, 13 union allselect 0, 5, 5 union allselect 1, 5, 6 union allselect 2, 5, 7 union allselect 3, 5, 8 union allselect 4, 5, 9 union allselect 5, 5, 10 union allselect 6, 5, 11 union allselect 7, 5, 12 union allselect 8, 5, 13 union allselect 9, 5, 14 union allselect 0, 6, 6 union allselect 1, 6, 7 union allselect 2, 6, 8 union allselect 3, 6, 9 union allselect 4, 6, 10 union allselect 5, 6, 11 union allselect 6, 6, 12 union allselect 7, 6, 13 union allselect 8, 6, 14 union allselect 9, 6, 15 union allselect 0, 7, 7 union allselect 1, 7, 8 union allselect 2, 7, 9 union allselect 3, 7, 10 union allselect 4, 7, 11 union allselect 5, 7, 12 union allselect 6, 7, 13 union allselect 7, 7, 14 union allselect 8, 7, 15 union allselect 9, 7, 16 union allselect 0, 8, 8 union allselect 1, 8, 9 union allselect 2, 8, 10 union allselect 3, 8, 11 union allselect 4, 8, 12 union allselect 5, 8, 13 union allselect 6, 8, 14 union allselect 7, 8, 15 union allselect 8, 8, 16 union allselect 9, 8, 17 union allselect 0, 9, 9 union allselect 1, 9, 10 union allselect 2, 9, 11 union allselect 3, 9, 12 union allselect 4, 9, 13 union allselect 5, 9, 14 union allselect 6, 9, 15 union allselect 7, 9, 16 union allselect 8, 9, 17 union allselect 9, 9, 18;declare @Num1 int, @Num2 int;select @Num1 = 1, @Num2 = 5;select Sum12 as [Sum]from #Sumswhere Num1 = @Num1and Num2 = @Num2;[/code]The table can be extended pretty much indefinitely, of course.[/quote]I did say no Assignment operators, so that disqualifies this statement:[code]select @Num1 = 1, @Num2 = 5;[/code]</description><pubDate>Fri, 17 Apr 2009 20:27:04 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Heh.  At least someone remembers their 3rd grade Addition tables!  :-)</description><pubDate>Fri, 17 Apr 2009 15:09:05 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]Michael Valentine Jones (4/17/2009)[/b][hr]OK, as long as we are doing useless SQL, I want to add numbers together without using any Arithmetic, Logical, Assignment, String, Bitwise, or Unary Operators or any Mathematical or Aggregate Functions.[/quote][code]create table #Sums (Num1 int not null,Num2 int not null,Sum12 int);insert into #Sums (Num1, Num2, Sum12)select 0, 0, 0 union allselect 1, 0, 1 union allselect 2, 0, 2 union allselect 3, 0, 3 union allselect 4, 0, 4 union allselect 5, 0, 5 union allselect 6, 0, 6 union allselect 7, 0, 7 union allselect 8, 0, 8 union allselect 9, 0, 9 union allselect 0, 1, 1 union allselect 1, 1, 2 union allselect 2, 1, 3 union allselect 3, 1, 4 union allselect 4, 1, 5 union allselect 5, 1, 6 union allselect 6, 1, 7 union allselect 7, 1, 8 union allselect 8, 1, 9 union allselect 9, 1, 10 union allselect 0, 2, 2 union allselect 1, 2, 3 union allselect 2, 2, 4 union allselect 3, 2, 5 union allselect 4, 2, 6 union allselect 5, 2, 7 union allselect 6, 2, 8 union allselect 7, 2, 9 union allselect 8, 2, 10 union allselect 9, 2, 11 union allselect 0, 3, 3 union allselect 1, 3, 4 union allselect 2, 3, 5 union allselect 3, 3, 6 union allselect 4, 3, 7 union allselect 5, 3, 8 union allselect 6, 3, 9 union allselect 7, 3, 10 union allselect 8, 3, 11 union allselect 9, 3, 12 union allselect 0, 4, 4 union allselect 1, 4, 5 union allselect 2, 4, 6 union allselect 3, 4, 7 union allselect 4, 4, 8 union allselect 5, 4, 9 union allselect 6, 4, 10 union allselect 7, 4, 11 union allselect 8, 4, 12 union allselect 9, 4, 13 union allselect 0, 5, 5 union allselect 1, 5, 6 union allselect 2, 5, 7 union allselect 3, 5, 8 union allselect 4, 5, 9 union allselect 5, 5, 10 union allselect 6, 5, 11 union allselect 7, 5, 12 union allselect 8, 5, 13 union allselect 9, 5, 14 union allselect 0, 6, 6 union allselect 1, 6, 7 union allselect 2, 6, 8 union allselect 3, 6, 9 union allselect 4, 6, 10 union allselect 5, 6, 11 union allselect 6, 6, 12 union allselect 7, 6, 13 union allselect 8, 6, 14 union allselect 9, 6, 15 union allselect 0, 7, 7 union allselect 1, 7, 8 union allselect 2, 7, 9 union allselect 3, 7, 10 union allselect 4, 7, 11 union allselect 5, 7, 12 union allselect 6, 7, 13 union allselect 7, 7, 14 union allselect 8, 7, 15 union allselect 9, 7, 16 union allselect 0, 8, 8 union allselect 1, 8, 9 union allselect 2, 8, 10 union allselect 3, 8, 11 union allselect 4, 8, 12 union allselect 5, 8, 13 union allselect 6, 8, 14 union allselect 7, 8, 15 union allselect 8, 8, 16 union allselect 9, 8, 17 union allselect 0, 9, 9 union allselect 1, 9, 10 union allselect 2, 9, 11 union allselect 3, 9, 12 union allselect 4, 9, 13 union allselect 5, 9, 14 union allselect 6, 9, 15 union allselect 7, 9, 16 union allselect 8, 9, 17 union allselect 9, 9, 18;declare @Num1 int, @Num2 int;select @Num1 = 1, @Num2 = 5;select Sum12 as [Sum]from #Sumswhere Num1 = @Num1and Num2 = @Num2;[/code]The table can be extended pretty much indefinitely, of course.</description><pubDate>Fri, 17 Apr 2009 13:32:00 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>OK, as long as we are doing useless SQL, I want to add numbers together without using any Arithmetic, Logical, Assignment, String, Bitwise, or Unary Operators or any Mathematical or Aggregate Functions.</description><pubDate>Fri, 17 Apr 2009 13:07:48 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Pinal Dave's reverse function has this code in it:RETURN (REVERSE(@StringToReverse))</description><pubDate>Fri, 17 Apr 2009 10:17:33 GMT</pubDate><dc:creator>ChrisM@Work</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]jcrawf02 (4/17/2009)[/b][hr][quote][b]GSquared (4/16/2009)[/b][hr][quote][b]Michael Valentine Jones (4/15/2009)[/b][hr]Maybe you should learn to use what is available in SQL Server, like REVERSE, instead of spending time duplicating what is already available.[/quote]Nah.  One of the best ways to learn the tricky parts of any engineering tool (and programming languages are definitely engineering tools) is to work out how to do something without using a pre-built answer.Nails and screws are awefully convenient, but it's fun and educational to build a wooden cabinet without using any.  Take away wood glue also, and it's a challenge for even a good woodworker, and fun.  And it teaches you techniques you can use in other applications that might be more practical.[/quote]Pshaw! That's no challenge, just use duct tape.[/quote]Hmm, interesting idea... :-)</description><pubDate>Fri, 17 Apr 2009 10:13:16 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>check out the below link, This will give you the function to reverse a string with out using REVERSE.http://blog.sqlauthority.com/2007/05/01/sql-server-user-defined-functions-udf-to-reverse-string-udf_reversestring/I always prefer to use existing functions, if they are available instead of duplicating the function.. but again on the other had if we try to duplicate the logic it will increase our programming capabilities :;-)I am confusing:hehe:</description><pubDate>Fri, 17 Apr 2009 09:09:58 GMT</pubDate><dc:creator>Vijaya Kadiyala</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]jcrawf02 (4/17/2009)[/b][hr][quote][b]GSquared (4/16/2009)[/b][hr][quote][b]Michael Valentine Jones (4/15/2009)[/b][hr]Maybe you should learn to use what is available in SQL Server, like REVERSE, instead of spending time duplicating what is already available.[/quote]Nah.  One of the best ways to learn the tricky parts of any engineering tool (and programming languages are definitely engineering tools) is to work out how to do something without using a pre-built answer.Nails and screws are awefully convenient, but it's fun and educational to build a wooden cabinet without using any.  Take away wood glue also, and it's a challenge for even a good woodworker, and fun.  And it teaches you techniques you can use in other applications that might be more practical.[/quote]Pshaw! That's no challenge, just use duct tape.[/quote]Well, I've found that when you try to use duct tape to reverse a character string, it leaves bits of glue all over your hard drives, and that's a bit of a problem...</description><pubDate>Fri, 17 Apr 2009 08:30:16 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]GSquared (4/16/2009)[/b][hr][quote][b]Michael Valentine Jones (4/15/2009)[/b][hr]Maybe you should learn to use what is available in SQL Server, like REVERSE, instead of spending time duplicating what is already available.[/quote]Nah.  One of the best ways to learn the tricky parts of any engineering tool (and programming languages are definitely engineering tools) is to work out how to do something without using a pre-built answer.Nails and screws are awefully convenient, but it's fun and educational to build a wooden cabinet without using any.  Take away wood glue also, and it's a challenge for even a good woodworker, and fun.  And it teaches you techniques you can use in other applications that might be more practical.[/quote]Pshaw! That's no challenge, just use duct tape.</description><pubDate>Fri, 17 Apr 2009 07:53:51 GMT</pubDate><dc:creator>jcrawf02</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]Michael Valentine Jones (4/15/2009)[/b][hr]Maybe you should learn to use what is available in SQL Server, like REVERSE, instead of spending time duplicating what is already available.[/quote]Nah.  One of the best ways to learn the tricky parts of any engineering tool (and programming languages are definitely engineering tools) is to work out how to do something without using a pre-built answer.Nails and screws are awefully convenient, but it's fun and educational to build a wooden cabinet without using any.  Take away wood glue also, and it's a challenge for even a good woodworker, and fun.  And it teaches you techniques you can use in other applications that might be more practical.</description><pubDate>Thu, 16 Apr 2009 07:01:50 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Maybe you should learn to use what is available in SQL Server, like REVERSE, instead of spending time duplicating what is already available.</description><pubDate>Wed, 15 Apr 2009 16:09:47 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>My question is not a homework question. :)My background is BI Development on Oracle, Teradata, Unix platforms. I am simply learning MS SQL Server and T-SQL.And thanks for all your help!Pit.</description><pubDate>Wed, 15 Apr 2009 15:48:20 GMT</pubDate><dc:creator>pshvets</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>This is actually from Jacob Sebastiens Challenge #3, where the actual rules were: "Write a single query that can reverse the strings in this column without using the REVERSE() function".</description><pubDate>Wed, 15 Apr 2009 13:53:42 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>I had that same point in my original post, but it got lost when my browser crashed mid-post.  I think it's just a game to not use Reverse.  Quite possibly a homework question, since they are often written with that kind of arbitrary rule.</description><pubDate>Wed, 15 Apr 2009 12:12:57 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]pshvets (4/14/2009)[/b][hr]Hello all.I am trying to write a function which reverses passed string WITHOUT using any built-in functionsSo if 'abc' is passed, it returns 'cba'I want to use recursion. Here is what I got:create function StringReverse( @InString varchar(20))returns varchar(20)ASbegindeclare @RevString varchar(20)IF len(@InString) in (0,1)	set @RevString = @InStringELSEset @RevString =	(		dbo.StringReverse(substring(@InString, len(@InString)/2+1, len(@InString))		+		dbo.StringReverse(substring(@InString, 1, len(@InString)/2)))	)return @RevStringendIt compiles fine, but when I call it, it throws an exception:select dbo.StringReverse('abc')Msg 217, Level 16, State 1, Line 1Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).Why? Function calls itself at most 5 times:Pass1 - RevString('c') + RevString('ab')Pass2 - 'c' + (RevString(RevString('b') + RevString('a'))Pass3 - 'cba'Should I use a loop?Thanks in advance for your help!Pit[/quote]The requirement of “Reverse string without built in functions” doesn’t make any sense, especially since all of the solutions posted, including yours, use built in functions.If it’s OK to use the built in DATALENGTH, SUBSTRING, RIGHT, LEFT, or LEN functions, why can’t you just use the built in REVERSE function?</description><pubDate>Wed, 15 Apr 2009 12:06:08 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Here's the sampe I came up with.[code]declare @Str varchar(100), @StrRev varchar(100);select @Str = 'able was I ere I saw elba';select @StrRev = coalesce(@StrRev + substring(@Str, number, 1), substring(@Str, number, 1))from dbo.Numberswhere number between 1 and len(@Str)order by number desc;select @Str, @StrRev;[/code]</description><pubDate>Wed, 15 Apr 2009 12:05:23 GMT</pubDate><dc:creator>GSquared</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>FYI, my example is recursive.</description><pubDate>Wed, 15 Apr 2009 11:40:13 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]Lowell (4/14/2009)[/b][hr][quote][b]pshvets (4/14/2009)[/b][hr]Hmmm...Does it mean that recursion cannot be used in this example? Or it should be implemented differently?Thanks,Pit[/quote]recursion has it's place, of course, but it does not serve string manipulation very well.without using a tally table, i'd do it like this: just a simple loop and string concatenation:[code]ALTER function CharReversal(@inputstring varchar(max))returns varchar(max)WITH SCHEMABINDINGASBEGIN  DECLARE @i int,           @Results varchar(max)  SET @Results=''  SET @i = 1  WHILE @i &amp;lt;= DATALENGTH(@inputstring)    BEGIN      SET @Results = SUBSTRING(@inputstring,@i,1) + @Results      SET @i=@i + 1    ENDRETURN @ResultsENDselect dbo.CharReversal('abc123xyz')--Results:zyx321cba[/code][/quote]Thank you for suggestion.I am still trying to figure out how to use recursion in this example. I am reading Joe Celko's book "Trees and Hierarchies in SQL for Smarties" and trying to write in T-SQL what he wrote for Oracle sql.Thanks,Pit.</description><pubDate>Wed, 15 Apr 2009 10:40:48 GMT</pubDate><dc:creator>pshvets</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Here's the code that I sent to Jacob Sebastian's challenge last month.  Minus the two syntax errors that probably eliminated me from consideration :( (gawd, I need a vacation!):[code]/* TSQL Challenge #3    Tested on SQL Server 2008    By RBarryYoung, March 28, 2009*/DECLARE @t TABLE( ID INT IDENTITY, data VARCHAR(MAX))INSERT INTO @t(data) SELECT 'Jacob'INSERT INTO @t(data) SELECT 'Sebastian';WITH cteReverseRecur as (    Select ID     , RIGHT( data, 1 ) as RevStr     , LEFT( data, LEN([data])-1 ) as RemStr     From @t  UNION ALL    Select ID     , RevStr + RIGHT( RemStr, 1 )     , Left( RemStr, LEN(RemStr)-1 )     From cteReverseRecur     Where RemStr &amp;gt; '')SELECT ID, RevStr as data From cteReverseRecur Where RemStr = '';[/code]</description><pubDate>Tue, 14 Apr 2009 20:06:51 GMT</pubDate><dc:creator>RBarryYoung</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>[quote][b]pshvets (4/14/2009)[/b][hr]Hmmm...Does it mean that recursion cannot be used in this example? Or it should be implemented differently?Thanks,Pit[/quote]recursion has it's place, of course, but it does not serve string manipulation very well.without using a tally table, i'd do it like this: just a simple loop and string concatenation:[code]ALTER function CharReversal(@inputstring varchar(max))returns varchar(max)WITH SCHEMABINDINGASBEGIN  DECLARE @i int,           @Results varchar(max)  SET @Results=''  SET @i = 1  WHILE @i &amp;lt;= DATALENGTH(@inputstring)    BEGIN      SET @Results = SUBSTRING(@inputstring,@i,1) + @Results      SET @i=@i + 1    ENDRETURN @ResultsENDselect dbo.CharReversal('abc123xyz')--Results:zyx321cba[/code]</description><pubDate>Tue, 14 Apr 2009 19:45:55 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Hmmm...Does it mean that recursion cannot be used in this example? Or it should be implemented differently?Thanks,Pit</description><pubDate>Tue, 14 Apr 2009 19:00:15 GMT</pubDate><dc:creator>pshvets</dc:creator></item><item><title>RE: Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>yeah, i think  you'll need to use a loop from 1 to datalength(@inputstring), and appending the char in reverse order.recursively calling a string that fiddles with one or two chars is going to hit the issue you already identified, max number of nesting levels.</description><pubDate>Tue, 14 Apr 2009 18:45:30 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>Reverse string without built in functions</title><link>http://www.sqlservercentral.com/Forums/Topic697178-23-1.aspx</link><description>Hello all.I am trying to write a function which reverses passed string WITHOUT using any built-in functionsSo if 'abc' is passed, it returns 'cba'I want to use recursion. Here is what I got:create function StringReverse( @InString varchar(20))returns varchar(20)ASbegindeclare @RevString varchar(20)IF len(@InString) in (0,1)	set @RevString = @InStringELSEset @RevString =	(		dbo.StringReverse(substring(@InString, len(@InString)/2+1, len(@InString))		+		dbo.StringReverse(substring(@InString, 1, len(@InString)/2)))	)return @RevStringendIt compiles fine, but when I call it, it throws an exception:select dbo.StringReverse('abc')Msg 217, Level 16, State 1, Line 1Maximum stored procedure, function, trigger, or view nesting level exceeded (limit 32).Why? Function calls itself at most 5 times:Pass1 - RevString('c') + RevString('ab')Pass2 - 'c' + (RevString(RevString('b') + RevString('a'))Pass3 - 'cba'Should I use a loop?Thanks in advance for your help!Pit</description><pubDate>Tue, 14 Apr 2009 18:40:58 GMT</pubDate><dc:creator>pshvets</dc:creator></item></channel></rss>