Home > Database > Mysql Tutorial > body text

How to Retrieve the First Day of the Corresponding Month in MySQL?

Barbara Streisand
Release: 2024-11-09 10:08:02
Original
281 people have browsed it

How to Retrieve the First Day of the Corresponding Month in MySQL?

Retrieving First Day of Corresponding Month in MySQL

In MySQL, you may encounter scenarios where you need to extract the first day of the month corresponding to a specified date. For instance, if a user selects '2010-06-15', the query should fetch results starting from '2010-06-01', not '2010-06-15'.

To calculate the first day from a given date, you can employ the following MySQL query:

select CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE);
Copy after login

This query utilizes the DATE_FORMAT function to extract the year and month portions of the current date in the format 'YYYY-MM-01'. The CAST function then converts the string to a DATE data type.

By leveraging this query, you can retrieve the first day of the current month, regardless of the selected date. This approach is particularly useful in scenarios where you need to perform date-related calculations or generate date-based reports.

The above is the detailed content of How to Retrieve the First Day of the Corresponding Month in 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