I have a SQL query and I need to order the result in ASC, but I need the numbers and spaces to be in the last position of the result, not first.
SELECT title FROM movie ORDER BY title ASC
Using the code above I get the titles starting from [space], 0-9, A,B,C... I need this form: A,B,C,...,Z,[anything else]
Thanks for any help.
We can use ROW_NUMBER to provide row number in a specified column based on Order By clause. In the following query, we want to get row number for SickLeaveHours column values in ascending order.
SQL Order by Alphabetical can be done on character-based column values using simply ORDER BY clause in ascending order. In SQL, various clauses can be used with the SELECT clause to achieve some specific functionality or make the resultset to be retrieved in a particular format.
If you want to sort some of the data in ascending order and other data in descending order, then you would have to use the ASC and DESC keywords. SELECT * FROM table ORDER BY column1 ASC, column2 DESC; That is how to use the ORDER BY clause in SQL to sort data in ascending order.
ORDER BY CASE
WHEN LOWER(LEFT(title, 1)) BETWEEN 'a' AND 'z' THEN 0
ELSE 1
END,
title
This is a bit of a hack, but it should work.
SELECT title FROM movie
ORDER BY CASE WHEN title LIKE '[0-9]%' THEN 'ZZZ' + title ELSE title END ASC
The idea is to make your order clause transform the items starting with numbers by prepending them with ZZZ. As for the items starting with a space, you may just want to clean them up with LTRIM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With