Solved

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

Posted on 2004-09-02
9
214 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
 

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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now