Below are examples of using SQL to return the first day of the month across various DBMSs.
This could be the first day of the current month, or the first day of a month based on a given date.
In SQLite, we can use the DATE() function to subtract one or more months from a date.
For datetime values, we can use the DATETIME() function.
We can use SQLite’s DATE() function to return the first, second, third, fourth, etc instance of a given day within a given year.
We can use the same technique to return the first Monday of each month in the year.
It’s not limited to Monday though. The same techniques can be used on Tuesday, Wednesday, Thursday, Friday, etc.
In PostgreSQL, we can use the + operator to add one or more seconds to a time value.
By “time” value, this could be an actual time value, a timestamp, or an interval. We can also add seconds to a date value or a date and time combination.
In SQLite, we can use the TIME() function to subtract one or more seconds from a time value.
For datetime values, we can use the DATETIME() function.
In SQLite, we can use the TIME() function to subtract one or more hours from a time value.
For datetime values, we can use the DATETIME() function.
Here are three options for returning the first day of a month in MariaDB.
This could be the first day of the current month, or the first day of the month based on a date that we specify.
Here’s an example of returning the first day of a given month in Oracle Database. We can return the first day of the current month, a previous or later month, or the first day of a month based on a specified date.
In SQLite, we can use the DATE() function to add one or more years to a date.
For datetime values, we can use the DATETIME() function.