Solved

Can I custom sort a query without adding a field for sort order?

Posted on 2004-09-02
9
237 Views
Last Modified: 2008-03-17
I have a query and of course I can sort on the field ascending or descending. Rather than alphabetical, I want to sort first by D, then by S, then by etc.

Is there a way to Order by this field:  dbo.Tbl_Trailer_Info.CurrentLocation but do something like .CurrentLocation "D", "S", (I know that syntax won't work, just trying to give you an idea of what I'm looking for) ?
0
Comment
Question by:jamesh1031
  • 5
  • 4
9 Comments
 
LVL 9

Accepted Solution

by:
paelo earned 500 total points
ID: 11963261
ORDER BY CASE WHEN CurrentLocation='D' THEN 0 WHEN CurrentLocation='S' THEN 1 ELSE 2 END


Or, if you want to specify a larger order hierarchy, create a function to convert them to numerical values and you can call that function from the ORDER BY clause.

-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11963290
0 is the first, what does the else 2 do?
0
 
LVL 9

Expert Comment

by:paelo
ID: 11963424
For all other values that aren't 'D' or 'S'.

-Paul.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:jamesh1031
ID: 11963580
One more question. I actually have things with names like door_19 door_20 and I do want the "D's" to come first, do I do some kind of CurrentLocation='D?', then 0 when currentlocation="s?' then 1 etc
0
 
LVL 9

Expert Comment

by:paelo
ID: 11963871
ORDER BY CASE WHEN CurrentLocation LIKE 'D%' THEN 0 WHEN CurrentLocation LIKE 'S%' THEN 1 ELSE 2 END

-Paul.
0
 
LVL 9

Expert Comment

by:paelo
ID: 11964011
Thanks.

Keep in mind that this will only sort on the first character in the field.  If you need something more complicated (with several levels, multiple sort criteria) I think the best solution is to create a function with which you can rank fields numerically according to their alphanumeric contents.

-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11964049
Can you tell me what you mean by "create a function"?

I did this which does sort the D's etc.

ORDER BY CASE WHEN dbo.Lkp_Location.Name LIKE 'D%' THEN 0 WHEN dbo.Lkp_Location.Name LIKE 'A%' THEN 1 WHEN dbo.Lkp_Location.Name LIKE 'B%' THEN
                       2 WHEN dbo.Lkp_Location.Name LIKE 'L%' THEN 3 WHEN dbo.Lkp_Location.Name LIKE 'S%' THEN 4 ELSE 5 END

But I have door numbers like Door_19 and you're right I don't get the doors in numerical order.

You answered my question, I probably should post a second one.
0
 
LVL 9

Expert Comment

by:paelo
ID: 11964468
Try this:

ORDER BY CASE WHEN dbo.Lkp_Location.Name LIKE 'D%' THEN 0 WHEN dbo.Lkp_Location.Name LIKE 'A%' THEN 1 WHEN dbo.Lkp_Location.Name LIKE 'B%' THEN 2 WHEN dbo.Lkp_Location.Name LIKE 'L%' THEN 3 WHEN dbo.Lkp_Location.Name LIKE 'S%' THEN 4 ELSE 5 END, dbo.Lkp_Location.Name


You'll see that I added your field as the second sort criteria.  This means it will sort by the numerical values you've imposed with the CASE statement first (D, A, B, L, S, etc.) and then it will sort on the field itself.  So all the D's will be sorted ascendingly.

Creating a user-defined function in SQLS will allow you to do a number of operations on a passed alphanumeric (such as Lkp_Location.name) and return a numerical value which represents its sort value.  You can create functions using the following syntax:

CREATE FUNCTION dbo.udfGetSortOrder( @Name varchar(200) )
RETURNS int
AS
BEGIN
...
END



And you can input it into your ORDER BY clause like this:

ORDER BY dbo.udfGetSortOrder(dbo.Lkp_Location.Name)


But you're right, this is probably more appropriate in another question.


-Paul.
0
 

Author Comment

by:jamesh1031
ID: 11964999
OK, I'm posting now, go and answer
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question