C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
MySQL Date/Time last_day() FunctionThe last_day() is a MySQL date/time function that accepts date or datetime value only. It is used to get the last day of the given month for the input date. If we provide an invalid input date or datetime, it always returns a NULL value. SyntaxThe following is the syntax used to return the last date of the given month. mysql> SELECT LAST_DAY(date); Parameter ExplanationDate: The last_day function accepts only one argument, either date or datetime, to get the last day date. Return ValueThis function returns the last day of the month for a given valid date argument. If the argument is invalid, we will get a NULL value. MySQL last_day() Function ExamplesLet us understand how to use the last_day() function in MySQL with the help of various examples. Example 1The below statement is a simple example of this function that returns the last day of the October month: mysql> SELECT LAST_DAY('2019-10-05'); See the below output image: Example 2If we want to get the last date of the month using datetime format, we can use the last_day() function in the following way: mysql> SELECT LAST_DAY('2019-10-05 10:25:05'); See the below output image: Example 3The last_day() function can also be used to get the last day of the current month. We can do this by combining the last_day() function with the NOW() or CURDATE() function like the below statements: mysql> SELECT LAST_DAY(NOW()); OR, mysql> SELECT LAST_DAY(CURDATE()); See the below output image: Example 4The last_day() function can also be used to get the last day of the next month. We can do this by adding one month to the current date and then pass the result in the last_day() function. See the below statement: mysql> SELECT LAST_DAY(CURDATE() + INTERVAL 1 MONTH); See the below output image: Example 5MySQL does not provide a function to get the first day of an input date. However, we can get the first day of the given date with the help of a last_day() function. We can do this using these points:
The below statement explains how to get the first day of the month of the given date. SELECT DATE_ADD(DATE_ADD(LAST_DAY('2020-01-15'), INTERVAL 1 DAY), INTERVAL - 1 MONTH) AS first_day; See the below output image:
Next TopicMySQL datetime
|