Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - order by varchar columns

Tags:

sql

I have a table containing a varchar column for months. Because of some reasons I have to save Persian months in it like Farvardin , Ordibehesh , ... . Now I want to select some rows from that table and I want to order my rows by months. what should I do ? Please just use SQL commands.

like image 254
Mohsen Nemati Avatar asked Sep 28 '22 05:09

Mohsen Nemati


1 Answers

You need to create a custom sort order with:

SELECT *
FROM months
ORDER BY CASE
          WHEN monthName = 'Farvardin' THEN '1'
          WHEN monthName = 'Ordibehesh' THEN '2'
          ...
          ELSE monthName END ASC
like image 77
Max Avatar answered Oct 13 '22 01:10

Max