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:
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:
The resulting value is provided below:
-------------------+
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:
This should return, as follows:
-------------------------+
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.
Output:
-------------------+--------+-------------------+
2022-10-24 11:44:06| 0|2022-10-24 11:44:06|
Using sysdate:
Output:
--------------------------+--------+--------------------------+
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.
Source: linuxhint.com