How to use the CONCAT() and STR_TO_DATE() Functions In MySQL
Syntax:
CONCAT(YEAR(date_column), '-', MONTH(date_column), '-01') AS first_day
Example: Using the CONCAT() and STR_TO_DATE() Functions
In this particular method, to create a string that represents the first day of the month, we shall use CONCAT() function along with STR_TO_DATE() function.
SELECT
name,
joining,
STR_TO_DATE(CONCAT(YEAR(joining), '-', MONTH(joining), '-01'), '%Y-%m-%d') AS first_day
FROM
students limit 4;
Output:
Explanation:
- This string, ‘concat(year(joining)), “-“, month(Joining), “-01”)’ will creating a string in the format of ‘YYYY-MM-01’ that represents the first day of the month.
- STR_TO_DATE() changes this string into a date format.
How to Get First Day of Every Corresponding Month in MySQL?
In the MySQL database, we generally need to get the first day of a month from a certain date. While there are many tasks related to dates, one key job is figuring out the first day of each month is an important need. This is important in many places like finance reports or schedules.
This is usual for data analysis and reports. Getting the first day of every month in MySQL is very important for date-oriented tasks. In this article, we will learn how to get the first day in every corresponding month in MySQL using date functions.
Contact Us