Home >Database >Mysql Tutorial >How to Find the First Day of Each Month in a Given Year Using MySQL?
Getting the First Day of Each Month for a Given Year
In MySQL, you can obtain the first day of each month for a specific year using a straightforward query. Suppose you have a selected date, such as '2010-06-15'. You want to retrieve the first day of that month, which is '2010-06-01'.
To achieve this, employ the following MySQL query:
select CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE);
Replace 'NOW()' with your desired date if you want to calculate the first day of each month for a different year. For instance, to get the first day of each month for the year 2010, use the following query:
select CAST(DATE_FORMAT('2010-01-01' ,'%Y-%m-01') as DATE);
This query applies the DATE_FORMAT() function to format the date in the specified format, and then casts the result to a DATE data type using the CAST() function.
The above is the detailed content of How to Find the First Day of Each Month in a Given Year Using MySQL?. For more information, please follow other related articles on the PHP Chinese website!