Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SQL Order By statement to sort results case insensitive?

People also ask

How do you sort case-sensitive in SQL?

You can force a case-sensitive sort by using the keyword BINARY after ORDER BY clause. Because lower-case letters are considered to have lower priority than upper-case letters, the result from this query displays the lower-case product names first in descending order. Case-sensitive sort in ascending order.

How do you make a case-insensitive in SQL query?

Case insensitive SQL SELECT: Use upper or lower functions or this: select * from users where lower(first_name) = 'fred'; As you can see, the pattern is to make the field you're searching into uppercase or lowercase, and then make your search string also be uppercase or lowercase to match the SQL function you've used.

Is ORDER BY case-sensitive SQL?

SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive.

How do you query case-insensitive?

Introduction to SQL Case Insensitive SQL Case insensitivity is to use the query statements and the keywords tables and columns by specifying them in capital or small letters of alphabets. SQL keywords are by default set to case insensitive that means that the keywords are allowed to be used in lower or upper case.


You can also do ORDER BY TITLE COLLATE NOCASE.

Edit: If you need to specify ASC or DESC, add this after NOCASE like

ORDER BY TITLE COLLATE NOCASE ASC

or

ORDER BY TITLE COLLATE NOCASE DESC

You can just convert everything to lowercase for the purposes of sorting:

SELECT * FROM NOTES ORDER BY LOWER(title);

If you want to make sure that the uppercase ones still end up ahead of the lowercase ones, just add that as a secondary sort:

SELECT * FROM NOTES ORDER BY LOWER(title), title;

SELECT * FROM NOTES ORDER BY UPPER(title)