SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to reverse the string in sql server 2005
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

lata
Starting Member

6 Posts

Posted - 09/20/2011 :  09:47:00  Show Profile  Reply with Quote
Hi,
suppose the string is,
Hi How are you?

The output should be
you? are How Hi

Regards,
lc

ehorn
Flowing Fount of Yak Knowledge

USA
1630 Posts

Posted - 09/20/2011 :  10:02:09  Show Profile  Reply with Quote
Perhaps something like;

declare @string varchar(100); set @string = 'Hi How are you?'
declare @delimiter char(1); set @delimiter = ' '
declare @newString varchar(100); set @newString = ''

SELECT 
		@newString = @newString + ' ' + 
		LTRIM(RTRIM(NULLIF(SUBSTRING(@delimiter + @string + @delimiter, Number, CHARINDEX(@delimiter, @delimiter + @string + @delimiter, Number) - Number), '')))
	FROM 
		master..spt_values 
	WHERE 
		Type = 'P' 
			AND Number <= LEN(@delimiter + @string + @delimiter)
			AND SUBSTRING(@delimiter + @string + @delimiter, Number - 1, 1) = @delimiter
			AND CHARINDEX(@delimiter, @delimiter + @string + @delimiter, Number) - Number > 0
			AND number > 0
ORDER BY number DESC
			
SELECT @newString as newString
Go to Top of Page

flamblaster
Constraint Violating Yak Guru

380 Posts

Posted - 09/20/2011 :  10:10:30  Show Profile  Reply with Quote
If it's only going to be 4 words, you could use parsename:


declare @sql varchar(30)
set @sql='Hi How are you?'

SELECT
		PARSENAME(REPLACE(@sql,' ','.'),1)+' '+
		PARSENAME(REPLACE(@sql,' ','.'),2)+' '+
		PARSENAME(REPLACE(@sql,' ','.'),3)+' '+
		PARSENAME(REPLACE(@sql,' ','.'),4)
Go to Top of Page

rajarajan
Starting Member

India
48 Posts

Posted - 09/20/2011 :  13:10:48  Show Profile  Reply with Quote
YOU CAN USE THIS FUNCTION


select REVERSE('HI HOW ARE U ')
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/20/2011 :  13:15:41  Show Profile  Reply with Quote
quote:
Originally posted by rajarajan

YOU CAN USE THIS FUNCTION


select REVERSE('HI HOW ARE U ')


nope it will give output as U ERA WOH IH
which is not OP wants
OP was asking to reverse the sequences of words in string without reversing words themselves

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rajarajan
Starting Member

India
48 Posts

Posted - 09/20/2011 :  13:19:30  Show Profile  Reply with Quote
USE THIS

declare @sql varchar(40)
set @sql='Hi How are you?'

SELECT
PARSENAME(REPLACE(@sql,' ','.'),1)+' '+
PARSENAME(REPLACE(@sql,' ','.'),2)+' '+
PARSENAME(REPLACE(@sql,' ','.'),3)+' '+
PARSENAME(REPLACE(@sql,' ','.'),4)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 09/20/2011 :  13:21:35  Show Profile  Reply with Quote
quote:
Originally posted by rajarajan

USE THIS

declare @sql varchar(40)
set @sql='Hi How are you?'

SELECT
PARSENAME(REPLACE(@sql,' ','.'),1)+' '+
PARSENAME(REPLACE(@sql,' ','.'),2)+' '+
PARSENAME(REPLACE(@sql,' ','.'),3)+' '+
PARSENAME(REPLACE(@sql,' ','.'),4)


this is same suggestion as one given by flambaster on 09/20/2011 : 10:10:30

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rajarajan
Starting Member

India
48 Posts

Posted - 09/20/2011 :  13:32:31  Show Profile  Reply with Quote
SEE THIS LINK
http://www.sql-server-helper.com/tips/split-name.aspx
Go to Top of Page

russell
Pyro-ma-ni-yak

USA
5071 Posts

Posted - 09/20/2011 :  14:34:05  Show Profile  Visit russell's Homepage  Reply with Quote
Persistent huh?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000