Home > Software engineering >  How to get a list of months from created_at column (List of months having data in database)
How to get a list of months from created_at column (List of months having data in database)

Time:03-03

Suppose i have the column below:

02-03-2021
02-04-2021
02-04-2021
02-06-2021
02-08-2021
02-11-2021

What i need is an array of the months sth like : [03,04,06,08,11]

Any thought of a good practice? Thanks in advance

CodePudding user response:

The simple use of DISTINCT and the MONTH() function is all you need

SELECT DISTINCT MONTH(Column_Name) ORDER BY MONTH(Column_Name);

This will produce a resultset with ONE row per MONTH. Its then a simple process of fetching the resultset and you will have your array.

CodePudding user response:

I took @RiggsFolly 's advice and modified it the way it suits my problem best

SELECT DISTINCT MONTH(created_at) AS Month 
FROM my_table 
GROUP BY MONTH(created_at); 
  • Related