In MariaDB, the SYSDATE()
and NOW()
functions are similar in that they return the current date and time.
However, there is an important difference: SYSDATE()
returns the time that it executes, whereas NOW()
returns the time that the statement started executing.
And when run within a stored procedure or trigger, SYSDATE()
will return the time that it executes, whereas NOW()
will return the time that the stored procedure or trigger started executing.
Example
First, let’s look at how these two functions can produce different results.
SELECT
NOW(),
SYSDATE(),
SLEEP(5),
NOW(),
SYSDATE();
Result (using vertical output):
NOW(): 2021-05-27 09:22:08 SYSDATE(): 2021-05-27 09:22:08 SLEEP(5): 0 NOW(): 2021-05-27 09:22:08 SYSDATE(): 2021-05-27 09:22:13
Here, we used the SLEEP()
function to pause processing for five seconds before the second lot of function calls.
We can see that the second time SYSDATE()
was invoked, its return value was different to the first time. NOW()
on the other hand, returned the same value both times.
Other Considerations
There are a few other things to be mindful of with these two functions:
SET TIMESTAMP
doesn’t affectSYSDATE()
, whereas it does affect theNOW()
function.SYSDATE()
is unsafe for replication if statement-based binary logging is used. However, there are ways to deal with this, if required. You can use--sysdate-is-now
command is a non-default option to aliasSYSDATE()
toNOW()
to make it safe for replication. See the MariaDB documentation forSYSDATE()
for more information.