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

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Resorting Words

Author  Topic 

sigmas
Posting Yak Master

172 Posts

Posted - 2013-01-11 : 08:56:26
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

52326 Posts

Posted - 2013-01-11 : 09:05:17
[code]
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
[/code]

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-11 : 09:06:12
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

172 Posts

Posted - 2013-01-11 : 09:31:52
Thanks,
similar to [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=182028[/url]
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

52326 Posts

Posted - 2013-01-11 : 09:34:34
quote:
Originally posted by sigmas

Thanks,
similar to [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=182028[/url]
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

172 Posts

Posted - 2013-01-11 : 09:39:29
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

52326 Posts

Posted - 2013-01-11 : 10:16:59
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
   

- Advertisement -