The MySQL SYSDATE()
function returns the current date and time. The value is returned in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.
This function is similar to NOW()
but with a subtle difference. SYSDATE()
returns the time at which it executes. NOW()
returns the time at which the statement started executing.
Syntax
The syntax goes like this:
SYSDATE([fsp])
Where the (optional) fsp
argument specifies the fractional seconds precision for the return value.
Example
Here’s an example to demonstrate.
SELECT SYSDATE();
Result:
+---------------------+ | SYSDATE() | +---------------------+ | 2018-06-23 11:36:52 | +---------------------+
Fractional Seconds Precision
Here’s an example of using the fsp
argument to specify the fractional seconds precision for the return value.
SELECT SYSDATE(6);
Result:
+----------------------------+ | SYSDATE(6) | +----------------------------+ | 2018-06-23 11:37:05.515928 | +----------------------------+
Numeric Context
Here’s an example of using the SYSDATE()
function in a numeric context.
SELECT SYSDATE() + 0;
Result:
+----------------+ | SYSDATE() + 0 | +----------------+ | 20180623113717 | +----------------+
You can also use a nonzero value to add or subtract from the return value.
SYSDATE() vs NOW()
As mentioned, SYSDATE()
and NOW()
do almost the same thing, but with a slight difference. SYSDATE()
returns the time at which it executes. The NOW()
function, on the other hand, returns a constant time that indicates the time at which the statement began to execute.
For an example, see SYSDATE()
vs NOW()
in MySQL: What’s the Difference?