PostgreSQL offers an ORDER BY clause that helps us sort a table ascendingly or descendingly. It sorts the table based on a specific column or multiple columns. However, when it comes to sorting a table by month name, the ORDER BY clause doesn’t provide the expected results. This is because the ORDER BY clause treats the month name as a string, so it sorts the month name alphabetically instead of sorting it in proper month ordering/numbering.
This write-up will illustrate a detailed procedure to sort a table by month name in Postgres.
How to Sort a Table By Month Name in Postgres?
We all know that the “N” comes before “O”, however, the “November” comes after “October”. So, using a simple ORDER BY clause will sort the month names alphabetically. This means the ORDER BY clause will sort November before October, April before January, and so on. So how to tackle such issues?
Well! In Postgres, to sort a table by month name, the ORDER BY clause is used alongside the EXTRACT() and TO_DATE() functions. Moreover, 'Month' must be used as an argument to the EXTRACT() and TO_DATE() functions. Here is a simple syntax to order the table’s data by month name:
SELECT col_list FROM tab_name ORDER BY EXTRACT(MONTH FROM TO_DATE(col_name, 'Month'));
In the above syntax:
- The TO_DATE() function will convert the month name to the respective date(i.e. Equivalent month number).
- Next, the EXTRACT() function will extract/pull the month from the converted date value.
- Finally, the ORDER BY clause will sort the table’s data into ascending/descending order based on the extracted month.
You can also use the “DESC” option to sort the table’s data from higher month to lower month.
Example: Sort Table By Month
Follow the given instructions to sort the table by month in ascending or descending order:
Step 1: Sample Table
Execute the provided command to populate all the data of the “emp_dat” table:
SELECT * FROM emp_dat;
The above table maintains the default insertion order.
Step 2: Sort By Month Name in Ascending Order
Use the below SELECT command to sort the table’s data with respect to the month name:
SELECT * FROM emp_dat ORDER BY EXTRACT(MONTH FROM TO_DATE(joining_month, 'Month'));
From the above snippet, you can observe that the table’s data has been successfully sorted(ascendingly) according to the month's name.
Step 3: Sort By Month Name in Descending Order
Use the DESC option to sort the table in descending format:
SELECT * FROM emp_dat ORDER BY EXTRACT(MONTH FROM TO_DATE(joining_month, 'Month')) DESC;
From the above result set, you can clearly notice that the table’s data has been successfully sorted (descendingly) according to the month name.
Conclusion
To sort a table by month name use the ORDER BY clause alongside the EXTRACT() and TO_DATE() functions. 'Month' must be specified as an argument to the EXTRACT() and TO_DATE() functions. The “ASC” or “DESC” options can be utilized to sort the table’s data ascendingly or descendingly. This post has demonstrated a detailed procedure to sort a table by month name in Postgres.