Get the current date and time in MySQL
In this article, we will learn about NOW(), CURRENT_TIMESTAMP() (also written as CURRENT_TIMESTAMP), and SYSDATE() to get the current date and time in MySQL. We will also see a comparison between these three functions.
Get the current date and time in MySQL
We have three methods to get the current date and time in MySQL. These methods include NOW(), CURRENT_TIMESTAMP() also written as CURRENT_TIMESTAMP, SYSDATE(). We can easily use them in INSERT statements as shown below.
Sample code:
# use NOW()
INSERT INTO student_attendance(ID, ATTENDANCE)
VALUES(1, NOW());
# use CURRENT_DATETIME
INSERT INTO student_attendance(ID, ATTENDANCE)
VALUES(1, CURRENT_DATETIME);
# use SYSDATE()
INSERT INTO student_attendance(ID, ATTENDANCE)
VALUES(1, SYSDATE());
Since we have multiple ways to insert the current date and time, it is important to understand the difference between them.
It will clarify when and where to use one of the above methods. A brief comparison of NOW(), CURRENT_TIMESTAMP(), and SYSDATE() is given below.
NOW() Function
The date and time returned by the method NOW() depends on the context we use it in. For example, if the context is a string, the NOW() method returns the date and time in string format like "YYYY-MM-DD HH-MM-SS".
If the function is used with a numeric context, it returns in the numeric format of YYYYMMDDHHMMSS. It returns a constant time showing the time when this particular statement started executing.
It is available and can be used in MySQL 4.0 and above.
CURRENT_TIMESTAMP() function
We can also write CURRENT_TIMESTAMP() as CURRENT_TIMESTAMP. It returns the current date and time as "YYYY-MM-DD HH-MM-SS" or YYYYMMDDHHMMSS if the function is used in a string context or a numeric context respectively.
Synonyms for CURRENT_TIMESTAMP include NOW() and CURRENT_TIMESTAMP() functions. Execute the following query to see the same output.
SELECT NOW(), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP();
Output:
NOW() CURRENT_TIMESTAMP CURRENT_TIMESTAMP()
2022-04-26 11:40:25 2022-04-26 11:40:25 2022-04-26 11:40:25
It is available in MySQL 4.0 and above. It also outputs a constant time, representing the time when this particular query started executing.
SYSDATE() function
Like the NOW()
and CURRENT_TIMESTAMP()
functions, it also returns the current date and time based on the context in which the function is used. The context can be a string or a number.
For string and numeric contexts, it returns "YYYY-MM-DD HH:MM:SS" and YYYYMMDDHHMMSS, respectively. The SYSDATE() function returns the exact time of query execution.
After reading the MySQL documentation, we can say that there is a subtle difference between SYSDATE() and NOW(). Remember that NOW(), CURRENT_TIMESTAMP, and CURRENT_TIMESTAMP() return the same results.
Execute the following query to notice the difference.
SELECT NOW(), SYSDATE(), CURRENT_TIMESTAMP(),
SLEEP(5), NOW(), SYSDATE(),CURRENT_TIMESTAMP();
Output:
Everything remains the same in the output given above except the results produced by the function before and after the function. The NOW() method returns a constant representing the time when the query started
executing SYSDATE()
.SLEEP()
In a trigger or stored procedure (also called stored function), NOW() outputs the time when the triggering statement or function began executing. SYSDATE() returns the exact time it ran (executed).
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:66 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:185 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:101 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca
MySQL Error Server PID File Could Not Be Found Solution
Publish Date:2025/04/22 Views:192 Category:MySQL
-
In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My
Get the last inserted ID using PHP MySQLi function
Publish Date:2025/04/22 Views:99 Category:MySQL
-
This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u