Unix Technical Forum

sorting string

This is a discussion on sorting string within the SQL Server forums, part of the Microsoft SQL Server category; --> hi!!! would appreciate if somebody could let me know the query/algoritham to sort the following values in the column ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 04:25 AM
balkii
 
Posts: n/a
Default sorting string

hi!!!

would appreciate if somebody could let me know the query/algoritham to
sort the following values in the column either in asc or desc

the values in the column originally

1
1-1/2
1-1/4
1/2
10
12
14
16
18
2
2-1/2
20
3
3/4
4
5
6
8

the sorted result should be as follows

1/2
3/4
1
1-1/4
1-1/2
2
2-1/2
3
4
5
6
8
10
12
14
16
18
20

TIA, have a great day

regards
bala
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 04:25 AM
David Portas
 
Posts: n/a
Default Re: sorting string

If these are supposed to be numeric values (1, 1.5, 1.25, 0.5, etc) then why
not store them as such? Make the column a NUMERIC, then format them as whole
numbers and fractions in the client application when you display or print
them. That way you'll find sorting and comparison much easier.

--
David Portas
SQL Server MVP
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 04:25 AM
Balakrishnan Ramanujam
 
Posts: n/a
Default Re: sorting string

hi david

well the requirement is such that it needs to be stored as string but
sorted as numeric. the sorting has to been done in the backend.

if it can stored as numeric life would have been simple since order by
would do the trick.

with the above mentioned conditions would appreciate if someone be
guiding light to attain the solution.

thanx
bala

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 04:25 AM
David Portas
 
Posts: n/a
Default Re: sorting string

I would create a table to hold the numeric and string equivalents, then
join that into your queries when you need it. You can use a Numbers
table to help create this conversion
(http://www.bizdatasolutions.com/tsql/tblnumbers.asp)

CREATE TABLE foo (fraction VARCHAR(10) PRIMARY KEY, value NUMERIC(10,2)
NOT NULL UNIQUE)

INSERT INTO foo (fraction, value)
SELECT CAST(N.num AS VARCHAR(10))+
CASE value WHEN 0 THEN '' ELSE '-' END +
fraction, N.num+value
FROM
(SELECT '',0 UNION ALL
SELECT '1/4',0.25 UNION ALL
SELECT '1/2',0.50 UNION ALL
SELECT '3/4',0.75 /* ... etc */
) AS F(fraction,value),
Numbers AS N
WHERE N.num<10

I assume you need to store the strings in this way in order to join them
with some other, similarly formatted data. I would still consider
storing them as numerics and then using the above as a joining table in
queries. If that doesn't suit then here is the ORDER BY:

SELECT col
FROM YourTable AS T1
JOIN Foo AS T2
ON T1.col = T2.fraction
ORDER BY T2.value

Hope this helps.

--
David Portas
SQL Server MVP
--

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 04:25 AM
Balakrishnan Ramanujam
 
Posts: n/a
Default Re: sorting string

hi david

thanx a ton, have a great weekend

regards
bala



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:55 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com