Home Forums Programming General Reverse string without built in functions RE: Reverse string without built in functions

  • Lowell (4/14/2009)


    pshvets (4/14/2009)


    Hmmm...

    Does it mean that recursion cannot be used in this example? Or it should be implemented differently?

    Thanks,

    Pit

    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:

    ALTER function CharReversal(@inputstring varchar(max))

    returns varchar(max)

    WITH SCHEMABINDING

    AS

    BEGIN

    DECLARE @i int,

    @Results varchar(max)

    SET @Results=''

    SET @i = 1

    WHILE @i <= DATALENGTH(@inputstring)

    BEGIN

    SET @Results = SUBSTRING(@inputstring,@i,1) + @Results

    SET @i=@i + 1

    END

    RETURN @Results

    END

    select dbo.CharReversal('abc123xyz')

    --Results:zyx321cba

    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.