DATETIME
MySQL

MySQL NOW Function: Get the Current Date and Time

intro

Let’s learn everything you need to know about the MySQL NOW function to retrieve the current date and time in your queries.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

In programming, having a quick way to retrieve the current date and time is essential for recording events, handling time-sensitive data, and more. Not surprisingly, most programming languages provide a standard function for getting the current timestamp. As you can imagine, that need extends to databases as well. This is exactly what the MySQL NOW() function is all about!

In this article, you will learn what the NOW MySQL function is, how to use it to fetch the current timestamp, and see practical examples of its application in SQL queries.

Let’s dive in!

What Is MySQL NOW?

In MySQL, NOW is a function that returns the current date and time. More precisely, it returns the date and time at which the execution of the SQL statement began.

That particular behavior must be taken into account when using the MySQL NOW function in stored functions or SQL triggers. No matter how much time passes from one statement involving NOW to the next, the value returned by the MySQL date function will always be the same. The reason is that NOW returns the date and time the stored function or trigger began to execute.

How To Use NOW in MySQL

This is the syntax of the MySQL NOW function:

Copy
        
1 NOW()

As you can see, it does not accept any arguments. Just like any other SQL function, it can be used in stored functions, triggers, and SELECT, INSERT, UPDATE, and DELETE statements. Specifically, it can appear in WHERE, GROUP BY, HAVING, and ORDER BY clauses.

When used in string contexts, NOW returns the current date and time in the DATETIME/TIMESTAMP format 'YYYY-MM-DD HH:MM:SS':

Copy
        
1 SELECT NOW();

This would return a string like this:

Copy
        
1 2024-07-02 09:12:59

Find out more about date formats in our guide on SQL date data types.

When used in numeric contexts, NOW returns the current date and time in the format YYYYMMDDHHMMSS:

Copy
        
1 SELECT NOW() + 0;

This time, the result will be something like this:

Copy
        
1 20240702091259

Note: In both cases, the returned value is expressed in the session time zone.

Analyzing the SQL data type of a cell becomes even easier with DbVisualizer's object view feature:

Seeing the type and format of a cell in DbVisualizer
Seeing the type and format of a cell in DbVisualizer

Just click on a cell, and DbVisualizer will give you its types and format. Discover all DbVisualizer features!

Keep in mind that the value returned by the NOW function can be used with INTERVAL to add or remove time from the given datetime, as in the following example:

Copy
        
1 SELECT NOW() + INTERVAL 1 HOUR;

The new result will be:

Copy
        
1 2024-07-02 10:12:59

Learn more in our guide on SQL add to date operations.

Note that the NOW MySQL function has three synonyms:

MySQL NOW Function: Use Cases

The most common use cases of the datetime NOW MySQL function includes:

  • Scheduling tasks or events based on the current date and time:
Copy
        
1 UPDATE tasks 2 SET scheduled_time = NOW() + INTERVAL 1 DAY 3 WHERE task_id = 15;
  • Finding records before or after the current time or in specific time intervals starting from the current time:
Copy
        
1 SELECT * FROM events 2 WHERE event_time BETWEEN NOW() AND NOW() + INTERVAL 2 WEEK;
  • Create timestamps in logs to track the timing of system events or errors:
Copy
        
1 INSERT INTO system_logs (event_type, event_details, log_time) 2 VALUES ('ERROR', 'Null pointer exception', NOW());
  • Insert the current date and time into a table:
Copy
        
1 INSERT INTO orders (user_id, product_id, quantity, order_time) 2 VALUES (14, 101, 3, NOW());
  • Calculate age or time differences using the current date and time:
Copy
        
1 SELECT username, TIMESTAMPDIFF(YEAR, birth_date, NOW()) AS age 2 FROM users;
  • Check for expired records:
Copy
        
1 DELETE FROM tokens 2 WHERE expiry_time < NOW();
  • Archive or delete records older than a certain period using the current timestamp (such behavior can be useful within functions):
Copy
        
1 DELETE FROM orders 2 WHERE order_date < NOW() - INTERVAL 1 YEAR;

Conclusion

In this guide, you learned what the MySQL NOW function is and how it works. You now know that NOW is a MySQL function that returns the date and time an SQL statement began to execute. Thanks to the examples shown here, you also saw when to use it in real-world scenarios.

NOW produces values with different formats depending on the context. As shown here, understanding the type and format of a value in a table cell is possible with just a click in a powerful SQL client like DbVisualizer. On top of that, this comprehensive database client supports several DBMS technologies, has advanced query optimization capabilities, and can generate ERD-type schemas with a single click. Try DbVisualizer for free!

FAQ

How to get the current date and time in MySQL?

If you want to get the date and time of when an SQL statement begins execution, use the NOW function. On the other hand, if you want to get the exact date and time—regardless of the execution time—you can use the SYSDATE function. This returns the exact time at which the function was executed.

What is the difference between the MySQL NOW and SYSDATE functions?

Both NOW and SYSDATE are MySQL date functions. NOW returns a constant time that indicates the time at which the statement began to execute, while SYSDATE returns the time at which the function executes. You can verify the different behavior of the two functions with the following query:

Copy
        
1 SELECT NOW() AS now_1, SLEEP(2), NOW() AS now_2, 2 SYSDATE() AS sysdate_1, SLEEP(2), SYSDATE() AS sysdate_2;

now_1 and now_2 will contain the same value, while sysdate_2 will contain a date with 2 seconds more than sysdate_1.

Is the MySQL NOW function part of the SQL standard?

The MySQL NOW function is not part of the SQL standard. Instead, the ANSI SQL standard specifies functions like CURRENT_TIMESTAMP, CURRENT_DATE, and CURRENT_TIME for similar purposes.

What is the MySQL timestamp NOW equivalent?

NOW already returns a TIMESTAMP, but the equivalent CURRENT_TIMESTAMP function can be used for improved clarity.

What is the MySQL date NOW equivalent?

NOW returns both the date and the time. If you want only the current date, you must call the MySQL CURRENT_DATE function.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

When, How, and Why to Use the Truncate SQL Statement

author Lukas Vileikis tags DbVisualizer MySQL SQL 6 min 2025-03-11
title

SQL PIVOT: How to Use a Pivot Table In SQL

author Leslie S. Gyamfi tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-03-10
title

MySQL ALTER TABLE Statement: Definitive Guide

author Antonello Zanini tags ALTER TABLE MySQL 11 min 2025-03-06
title

A Complete Guide to the SQL CREATE INDEX Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-05
title

A Complete Guide to the Order of Execution in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-03-03
title

A Complete Guide to the MySQL CTE Mechanism

author Antonello Zanini tags MySQL 10 min 2025-02-26
title

SQL Operators: A Complete Walkthrough

author Lukas Vileikis tags DbVisualizer MySQL SQL 4 min 2025-02-25
title

How to Truncate Many Tables In SQL at Once

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-02-24
title

Clustered vs Non-Clustered Index: Complete SQL Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-02-13
title

Queries and Subqueries in SQL: The Definitive Guide

author Lukas Vileikis tags DbVisualizer MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-02-12

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.