Reverse string with out using in build function

 Posted by Mcadeepuraj on 5/20/2011 | Category: Sql Server Interview questions | Views: 3421 | Points: 40
Answer:

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 = '';


Source: Interview | Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response