Site icon TestingDocs.com

MySQL NOW Function

Overview

In this tutorial, we will learn about MySQL NOW Function with some examples. The NOW() Function returns the current date and time. The result is displayed in ‘YYYY-MM-DD HH:MM:DD’ format.

Syntax

The general syntax for the NOW() function is:

NOW()

Examples

Simple example of the function usage are as follows:

mysql> SELECT NOW();
+———————+
| NOW() |
+———————+
| 2022-06-12 22:11:20 |
+———————+
1 row in set (0.00 sec)

mysql> SELECT NOW() + 7;
+—————-+
| NOW() + 7 |
+—————-+
| 20220612223947 |
+—————-+
1 row in set (0.00 sec)

IGNORE_SPACE

By default, there must be no whitespace between a function name and the parenthesis. However, we can change this default behavior by enabling the SET sql_mode to cause whitespace after the function name to be ignored.

This example contains a whitespace between a function name and the parenthesis. This leads to an error
message as shown below:

mysql> SELECT NOW ();

ERROR 1630 (42000): FUNCTION world.NOW does not exist. Check the ‘Function Name Parsing and Resolution’ section in the Reference Manual

mysql> SET sql_mode=’IGNORE_SPACE’;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT NOW ();
+———————+
| NOW () |
+———————+
| 2022-06-15 14:12:48 |
+———————+
1 row in set (0.01 sec)

 

Input to another Function

The NOW() Function’s output can be the input of another MySQL function. We can also invoke this function in other date functions. For example, to know the weekday that is from 100 days from now.

mysql> SELECT NOW() +INTERVAL 100 DAY;
+————————-+
| NOW() +INTERVAL 100 DAY |
+————————-+
| 2022-09-20 22:31:10 |
+————————-+
1 row in set (0.00 sec)

mysql> SELECT DAYNAME(NOW() +INTERVAL 100 DAY);
+———————————-+
| DAYNAME(NOW() +INTERVAL 100 DAY) |
+———————————-+
| Tuesday |
+———————————-+
1 row in set (0.00 sec)

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

 

Exit mobile version