MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
MySQL provides two functions to get the current date and time: NOW()
and SYSDATE()
.
NOW() Function:
The NOW()
function is used to return the current date and time.
Syntax:
NOW()
Example:
SELECT NOW();
This will return the current date and time. For example, 2023-05-12 15:45:32
.
SYSDATE() Function:
The SYSDATE()
function is also used to return the current date and time.
Syntax:
SYSDATE()
Example:
SELECT SYSDATE();
This will also return the current date and time. For example, 2023-05-12 15:45:32
.
Difference between NOW() and SYSDATE():
While NOW()
and SYSDATE()
may seem identical, there is a small but significant difference between them:
NOW()
function returns the time at which the statement began to execute.SYSDATE()
function returns the exact time at which it is executed.This difference only matters within stored programs (like stored procedures and functions) or within a single query that takes a long time to execute. In most other cases, you can use NOW()
and SYSDATE()
interchangeably.
Example showing the difference:
SELECT NOW(), SLEEP(5), NOW(), SYSDATE();
In the above query, SLEEP(5)
is used to pause execution for 5 seconds. The two NOW()
calls will return the exact same time because they're part of the same statement. However, the SYSDATE()
function will return the time at the exact moment it is executed, which will be about 5 seconds later than the time returned by NOW()
.
MySQL NOW function example:
SELECT NOW();
How to use NOW function in MySQL:
INSERT INTO table_name (column1, column2, timestamp_column) VALUES ('value1', 'value2', NOW());
Getting current timestamp with NOW in MySQL:
SELECT NOW() AS current_timestamp;
Difference between NOW and SYSDATE in MySQL:
SELECT NOW(), SYSDATE();
Using NOW for date and time values in MySQL:
UPDATE table_name SET timestamp_column = NOW() WHERE id = 1;
MySQL SYSDATE function example:
SELECT SYSDATE();
How to use SYSDATE function in MySQL:
INSERT INTO table_name (column1, column2, timestamp_column) VALUES ('value1', 'value2', SYSDATE());
Getting system date with SYSDATE in MySQL:
SELECT SYSDATE() AS system_date;
Comparing current timestamp functions in MySQL: NOW vs SYSDATE:
SELECT NOW() AS now_result, SYSDATE() AS sysdate_result;