MySQL MariaDB

MySQL SYSDATE() Function

In this post, we will discuss the sysdate() function in MySQL.

This function allows you to fetch the current date and time in the format of YYYY-MM-DD HH:MM:SS as a string. The function can also return the date and time as a numerical value in the format YYYYMMDDHHMMSS.

Now, let us explore.

Function Syntax

The function follows a syntax as depicted in the snippet below:

SYSDATE([fsp])

The fsp argument represents the fractional seconds precision. This optional value determines the precision of the seconds’ value. The precision value ranges from 0 to 6.

NOTE: The sysdate() function returns the time the function completes execution, unlike the now() function, which returns the time it starts the execution.

Example 1

The following example shows the sysdate() function usage:

select SYSDATE();

The resulting value is provided below:

SYSDATE()          |
-------------------+
2022-10-24 11:34:27|

Example 2: Using the sysdate() Function With FSP Parameter

The following example uses the sysdate() function with the fsp parameter:

select sysdate(5);

This should return, as follows:

sysdate(5)               |
-------------------------+
2022-10-24 11:38:55.32818|

The function should return the current time with five-point precision for the seconds’ value.

Example 3: Sysdate and Now()

The following example shows the actual difference between the sysdate() and now() functions.

select NOW(), SLEEP(3), NOW();

Output:

NOW()              |SLEEP(3)|NOW()              |
-------------------+--------+-------------------+
2022-10-24 11:44:06|       0|2022-10-24 11:44:06|

Using sysdate:

select SYSDATE(6), SLEEP(3), SYSDATE(6);

Output:

SYSDATE(6)                |SLEEP(3)|SYSDATE(6)                |
--------------------------+--------+--------------------------+
2022-10-24 11:44:15.030105|       0|2022-10-24 11:44:18.033804|

As we can see, the sysdate() function is a non-deterministic function.

Conclusion

In this post, we covered the basics of working with MySQL’s sysdate() function to get the current date and time. There were examples displaying the difference between the sysdate() function and the now() function.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list