Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table contains data as follows

COLA
-------------------------
Today is sunday
Hi how are you
Hello i am bharadwaj
Hi i am fine
Help me out


I Need output like as follows

COLA
-------------------------
Sunday is today
You are how hi
Bharadwaj am i hello
Fine am i hi
Out me help
Posted
Updated 3-Jun-15 10:14am
v2
Comments
ZurdoDev 3-Jun-15 16:32pm    
.you to luck Good

I can't think of a god way to do that. There is the REVERSE() function in SQL but it reverses the whole string. Is this just for fun?
Mathi Mani 3-Jun-15 16:40pm    
What are you trying to achieve by doing this?
Tomas Takac 3-Jun-15 16:44pm    
Writing a CLR function could be one way to do it.

Don't.
SQL has string handling which is - at best - poor.

Do that in your presentation language, where it's trivial. Leave SQL to do what it's best at - storing and retrieving data- and do string manipulation in languages which are designed for it!

For example, in C#, it's three lines of code!

C#
string[] words = lineOfText.Split(' ');
Array.Reverse(words);
string reversed = string.Join(" ", words);
 
Share this answer
 
OriginalGriff - basically - is right. Note that it's possible, even when the performance is poor (or nearly to poor)... ;)

If you want to reverse words in string, try this: SQL SERVER – Reverse String Word By Word[^]
 
Share this answer
 
try this!..

SQL
DECLARE @source VARCHAR(MAX)
DECLARE @dest VARCHAR(MAX)
DECLARE @lenght INT

SET @source = 'Today is sunday'
SET @dest = ''

WHILE LEN(@source) > 0
BEGIN
    IF CHARINDEX(' ', @source) > 0
    BEGIN
        SET @dest = SUBSTRING(@source,0,CHARINDEX(' ', @source)) + ' ' + @dest
        SET @source = LTRIM(RTRIM(SUBSTRING(@source,CHARINDEX(' ', @source)+1,LEN(@source))))
    END
    ELSE
    BEGIN
        SET @dest = @source + ' ' + @dest
        SET @source = ''
    END
END
SELECT @dest
 
Share this answer
 
Comments
Member 11366322 5-Jun-15 12:33pm    
Hi Maddy

Thanks for your replay. I know the way what you post.

But that one is static with one string. But I have table and it contains the several records and I want to reverse all those records at a time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900