How to use Intervals In MariaDB
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.
SELECT id, LAST_DAY(val) - interval 1 month + interval 1 day as first_day, LAST_DAY(val) AS last_day
FROM test;
Output:
Explanation: In the above query, to find the first_day we first subtracted 1 month using interval 1 month. This provides us with the last day of the previous month. We then add 1 day to the date using interval 1 day.
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.