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);
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!