Discussion Locked

SQL ORDER BY clause

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

When I run a query against an SQL server database with an ORDER BY clause, the result set is ordered "alphabetically":

Query:
SELECT Product_VAMID FROM Product ORDER BY Product_VAMID

1060-a
1060-s
11-a
11-s
1645-a
1645-s

I need to order it "numerically":

11-a
11-s
1060-a
1060-s
1645-a
1645-s

How can I achieve this? I have tried several different ways, and searched everywhere, but I cant seem to find a way. All help appreciated.
thanks in advance.

10 replies

by dklippert

Alpha fields like 10-A are always sorted alpha.
Set up a calculated sorting field like this:

Sorter:Left([Product_VAMID],Len([Product_VAMID])-2)

by madestroitsolutions

Your answer worked, but Bob Sellman provided the kind of answer I was looking for. Read my comments to him below if you wish to know out of curiosity how I achieved it based on his idea. Im still giving you points too. Thank you

by madestroitsolutions

Thanks for your answer DKLipper. Your answer only works if you add CAST:
CAST(Left([Product_VAMID],Len([Product_VAMID])-2) AS NUMERIC)
I am looking for a solution other than this one. I will wait to see If someone out there knows a different answer. If not I will close the question. I will still give you the points though, thanks a lot!

by no_problem

Did you try this:

SELECT Product_VAMID FROM Product ORDER BY Product_VAMID ASC

or

SELECT Product_VAMID FROM Product ORDER BY Product_VAMID DESC

Try it out.
Thanks.

by madestroitsolutions

Thank you for your reply, but I am talking about something far more advanced. Please read the description of the question, I have done this already. Thanks again.

by rrv

Let us assume fields Product_VAMID has a field length 8. Then the query would be

select convert(num(8),Product_VAMID) as newfield from Product order by newField

This query should work.

All the best
Raja Venkatesh
+971-50-5688186

by madestroitsolutions

I have tried this before. Here is the result of the query:
Server: Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Thank you for your answer anyways

by bob sellman

It may be too late to do this, but if you zero fill the number parts of the product IDs so they are all the same length, sorting will work properly. Based on your examples, the product numbers are a maximum of 4 characters, so that instead of "11-a", you should have "0011-a".

If all of your product IDs include a dash, then the easy way to do this is to create an SQL update query that will find the location of the "-" and, based on it's location, add an appropriate number of "0" (zeroes) to the front part of the ID.

Then make sure that all new data entered formats the product IDs with zero fill at the front of the number part of the ID so the number is always 4 characters long (or 5, if you need to be able to handle larger numbers).
The basic rule is that when sorting any kind of text you must place all characters in the same position, so that if part of the text is a number, the tens digit must be in the same physical location for every text.

Even if this involves some work at this point in time, you might still be better off making the change now to avoid further problems in the future.

by madestroitsolutions

Thank you Bob! Your answer indeed worked, although not exactly as you suggested. Rather than changing my data, I simply create an extra field when the query is issued, which serves as a sorter. Just in case you, or others are interested in the implementation of the sort based on your idea, here is the SQL statement:

SELECT
CASE
WHEN LEN(Product_VAMID)=20 THEN
Product_VAMID
ELSE
REPLICATE('0',20-LEN(Product_VAMID))+Product_VAMID
END
as SortVAMID, Product_VAMID FROM Product ORDER BY SortVAMID

The SortVAMID is the Temp field used to sort, while the Product_VAMID is the one displayed.

Thanks again,

Juan Romero
juanja01@optonline.net

This discussion is closed to new replies.