How to use YEAR, MONTH and LAST_DAY function In MariaDB

As we have understood earlier in this article, YEAR, MONTH and LAST_DAY functions allows us to manipulate the dates in different fashions. We can make use of the functions in conjunction with CONCAT_WS function to find the relevant values. The following query uses the above functions to find the values:

SELECT id, DATE(CONCAT_WS('-', YEAR(val), MONTH(val), '01')) AS first_day, LAST_DAY(val) AS last_day
FROM test;

Output:

first and last day of month

Explanation: In the above query, we have retrieved the respective values using the functions and then concatenated them with the ‘-‘ separator.

How to Get the First and Last Day of the Month in MariaDB?

In MariaDB, working with dates is a common task, and knowing how to manipulate them can be incredibly useful. One common requirement is to find the first and last day of the month for a given date. In this article, we’ll learn various methods like YEAR, MONTH, LAST_DAY and so on which are used to get the first and last day of the month.

Similar Reads

How to Get the First and Last Day of the Month?

To get the first and last day of the month in MariaDB, we can use several approaches. We’ll discuss three methods. We will fetch the first and last day of the month in Mariadb using the below methods are as follows:...

1. Using YEAR, MONTH and LAST_DAY function

As we have understood earlier in this article, YEAR, MONTH and LAST_DAY functions allows us to manipulate the dates in different fashions. We can make use of the functions in conjunction with CONCAT_WS function to find the relevant values. The following query uses the above functions to find the values:...

2. Using Intervals

We can use the interval keyword to subtract and add specified amount of days to a date object. The following query uses the same to get the required values....

3. Using DATE_FORMAT and LAST_DAY Function

As we have already seen earlier, the DATE_FORMAT function can be used to format the date in whatever fashion we want it to be. We can use the same function to get relevant values. The following query uses the same to get the required values....

Conclusion

In this article, we saw how we can find the first and last day of a month in MariaDB. We started by looking at several functions like YEAR, MONTH, LAST_DAY and DATE_FORMAT. We understood what they do and saw examples of them. We later saw several methods to find the required values. First we saw how to use the several functions, then saw intervals and finally we saw how we can use the DATE_FORMAT function....