TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MySQL last_day() Function

MySQL last_day() Function with Examples for beginners and professionals on mysql tutorial, mysql function, mysql date time, ADDDATE(), ADDTIME(), CONVERT_TZ(), CURDATE(), CURTIME(), DATE(), TIME() DATE_ADD(), DATE_FORMAT(), TIME_FORMAT(), TIMESTAMP(), TO_DAYS(), WEEK() etc.

<< Back to MYSQL

MySQL Date/Time last_day() Function

The 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.

Syntax

The following is the syntax used to return the last date of the given month.

mysql> SELECT LAST_DAY(date); 

Parameter Explanation

Date: The last_day function accepts only one argument, either date or datetime, to get the last day date.

Return Value

This 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 Examples

Let us understand how to use the last_day() function in MySQL with the help of various examples.

Example 1

The 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:

MySQL Datetime last_day() Function

Example 2

If 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:

MySQL Datetime last_day() Function

Example 3

The 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:

MySQL Datetime last_day() Function

Example 4

The 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:

MySQL Datetime last_day() Function

Example 5

MySQL 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:

  1. Get the month's last day of a given date.
  2. Then, add one day to get the next month's first date using the date_add() function.
  3. Finally, subtract one month from the resultant result to return the month's first day of the given date.

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:

MySQL Datetime last_day() Function
Next TopicMySQL datetime




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf