Solved

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

Posted on 2004-09-02
9
230 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access denied running PowerPivot -SQL Server 2014 on Windows Server 2012 10 28
Help Required 3 90
VB.net and sql server 4 33
Tsql query 6 18
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

813 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

12 Experts available now in Live!

Get 1:1 Help Now