Name

NOW()

Synopsis

NOW()

This function returns the current date and time. The format returned is yyyy-mm-dd hh:mm:ss.nnnnnn, unless the function is used in a numeric calculation. Then it will return the data in a yyyymmdd format. It’s synonymous with LOCALTIME() and LOCALTIMESTAMP(). Here is an example:

SELECT NOW( ) AS Now,
NOW( ) + 105008 AS '1 hour, 50 min., 8 sec. Later';

+---------------------+-------------------------------+
| Now                 | 1 hour, 50 min., 8 sec. Later |
+---------------------+-------------------------------+
| 2007-03-18 20:08:30 |         20070318305838.000000 | 
+---------------------+-------------------------------+

By adding 105,008 to the current time, the hour is increased by 1, the minutes by 50, and the seconds by 8, and the time is displayed in the second field without dashes. Notice that the results show the hours to be 30 now and not 6, and the date wasn’t adjusted. Raw adding of time is usually not a good alternative to functions such as DATE_ADD() or TIME_ADD().

The NOW() function is similar to the SYSDATE() function in that they both return the current datetime in the same format. However, the NOW() function returns the time the SQL statement began, whereas SYSDATE() returns the time the function was invoked. This can lead to differences when long triggers or stored procedures run; an embedded SYSDATE() will then reflect a later time than NOW(). For this reason, there are potential problems using SYSDATE() with regard to replication. See the description of SYSDATE() ...

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.