473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 20 '05 #1
4 1898
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
--
Jul 20 '05 #2
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!
Jul 20 '05 #3
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!
Jul 20 '05 #4
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!
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
12
by: pmud | last post by:
Hi, I am using teh following code for sorting the data grid but it doesnt work. I have set the auto generate columns to false. & set the sort expression for each field as the anme of that...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
10
by: Sjaakie | last post by:
Hi, I'm, what it turns out to be, fooling around with 3-tier design. At several websites people get really enthusiastic about using custom dataobjects instead of datasets/-tables. While trying to...
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
1
by: castron | last post by:
Hello All, I have a grid view that allows sorting, paging, editing, etc. Under On Load event, if I check: if(!IsPostBack){ DisplayData(); }, the Edit portion works fine. However, the Sorting...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.