"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Find the First Day of Each Month in a Given Year Using MySQL?

How to Find the First Day of Each Month in a Given Year Using MySQL?

Published on 2024-11-11
Browse:520

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.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3