TSQL Challenges

TSQL Challenges intend to help you to test and enhance SET based querying skills using TSQL.





TSQL Challenge 3

Congratulations to the winners of TSQL Challenge 2 and thanks to all the participants and readers who welcomed it with great enthusiasm. Here is the next challenge. This challenge is not for solving any business/application problem, but just to refresh your TSQL skills on set based operations.

So, the task is to reverse a string without using the REVERSE() function. What is wrong with the REVERSE() function? Nothing Really! As I mentioned earlier, this is to refresh your TSQL skills on set based operations. In real life, you should always use the REVERSE() function, if ever you need to reverse a string.

Again, we are not going to reverse a single string. We need to reverse all the values in the column of a table using a single query.

Here is the sample Data

ID          data
----------- --------------------
1           Jacob
2           Sebastian

Here is the expected result

id          data
----------- --------------------
2           naitsabeS
1           bocaJ

Use the script below to create the sample table.

DECLARE @t TABLE( ID INT IDENTITY, data VARCHAR(20))
INSERT INTO @t(data) SELECT 'Jacob'
INSERT INTO @t(data) SELECT 'Sebastian'

Notes:

  1. Write a single query that produces the expected result. No User Defined Functions allowed.
  2. Make sure that your code works with the sample script given above. Use the same column names, table variable name etc. This makes my life easier while testing the code.
  3. No restriction on SQL Server version. You can write the query for SQL Server 2000, 2005 or 2008
  4. Make sure that the subject of your email is ‘TSQL Challenge 3’
  5. Last date to submit your entries: 31 March 2009