Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Resorting Words
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 01/11/2013 :  08:56:26  Show Profile  Reply with Quote
Hi,
I need to resort(order asc) words in a string.

declare @s varchar(50) = 'one two three four seven nine'

The string must be after sorting like this:
'four nine one seven three two'

Is a simple method?

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/11/2013 :  09:05:17  Show Profile  Reply with Quote

declare @s varchar(50) = 'one two three four seven nine'
declare @sorder varchar(50)

SELECT @sorder= COALESCE(@sorder,'') + Val + ' '
FROM dbo.ParseValues(@s,' ') f

SELECT @sorder


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


Edited by - visakh16 on 01/11/2013 09:07:29
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/11/2013 :  09:06:12  Show Profile  Reply with Quote
parsevalues can be given in below link

http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html

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

Go to Top of Page

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 01/11/2013 :  09:31:52  Show Profile  Reply with Quote
Thanks,
similar to http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=182028
Posted - 01/11/2013 : 09:16:29
even word be sorted the query split all of strings, or just two first words need to order... .
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/11/2013 :  09:34:34  Show Profile  Reply with Quote
quote:
Originally posted by sigmas

Thanks,
similar to http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=182028
Posted - 01/11/2013 : 09:16:29
even word be sorted the query split all of strings, or just two first words need to order... .


sorry i dont understand your explanation
can you elaborate?

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

Go to Top of Page

sigmas
Posting Yak Master

Belarus
172 Posts

Posted - 01/11/2013 :  09:39:29  Show Profile  Reply with Quote
for example we want sort this:
'b a c d e f g h i j k'

Only 'a' and 'b' need to exchange but your query split all string then sort them.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 01/11/2013 :  10:16:59  Show Profile  Reply with Quote
how do you know only a and b are out of order unless you parse it. so you've to parse the entire string anyway using any of the methods

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

Go to Top of Page
  Previous Topic Topic Next 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