Home > Database > Mysql Tutorial > body text

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

Barbara Streisand
Release: 2024-11-08 21:49:02
Original
1023 people have browsed it

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);
Copy after login

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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template