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

  • 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!):

    /* 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 > '')

    SELECT ID, RevStr as data

    From cteReverseRecur

    Where RemStr = '';

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]