Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2004-09-02
Medium Priority
257 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
Question by:jamesh1031
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 5
• 4
9 Comments

LVL 9

Accepted Solution

paelo earned 2000 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

ID: 11963290
0 is the first, what does the else 2 do?
0

LVL 9

Expert Comment

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

-Paul.
0

Author Comment

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

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

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

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

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

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

## Featured Post

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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
###### Suggested Courses
Course of the Month9 days, 23 hours left to enroll

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

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