Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom order by in SQL server like P, A, L, H [closed]

Not ASC or DESC.... Order by custom...

I have tried using case but not successfully

SELECT * FROM Customers ORDER BY case country when 'P' then 1 … 

This is what I want:

like image 763
mhd noufel Avatar asked Oct 05 '13 10:10

mhd noufel


People also ask

How do I sort a custom order in SQL?

By default SQL ORDER BY sort, the column in ascending order but when the descending order is needed ORDER BY DESC can be used. In case when we need a custom sort then we need to use a CASE statement where we have to mention the priorities to get the column sorted.

How do I order last 3 letters in SQL?

SELECT `name` FROM `students` WHERE `marks` > 75 ORDER BY SUBSTR(`name`, -3), ID ASC; SUBSTR(name, -3) will select the last three characters in the name column of the student table.

Is ORDER BY ascending or descending SQL?

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.


1 Answers

SELECT * FROM Customers ORDER BY case when country = 'P' then 1               when country = 'A' then 2               when country = 'L' then 3               when country = 'H' then 4               else 5          end asc 
like image 189
juergen d Avatar answered Sep 18 '22 06:09

juergen d