The NOW()
function in MySQL is a powerful tool that returns the current date and time based on the server’s time zone. This function is widely used when you need to capture the exact moment an event occurs such as when recording timestamps for records like orders, deliveries, or log entries.
In this article, We will learn about the MYSQL NOW() function by understanding various examples in detail.
MySQL NOW() function
- The
NOW()
function in MySQL is used to return the current date and time based on the servers time zone.
- It provides the date and time in the format
YYYY-MM-DD HH:MM:SS
.
- The date and time returned by the NOW() function in MySQL are either in ‘YYYY-MM-DD HH:MM:SS’ format or ‘YYYYMMDDHHMMSS.uuuuuu’ format, depending on whether the function is used in a string or numeric context.
Syntax:
MySQL NOW function syntax is:
NOW()
Parameter
This method does not accept any parameters.
Examples of MySQL NOW() Function
Let’s look at some examples of the NOW function in MySQL. Learning the MySQL NOW function with examples will help you understand the concept better.
Example 1:
In this example, we are finding the current date and time using the NOW function. Here we will get the result in ‘YYYY-MM-DD HH:MM:SS’ format.
Query:
SELECT NOW()
AS CurrDateTime ;
Output :
CURRDATETIME |
2020-11-26 18:37:42 |
Example 2:
In this example, we are getting the current date and time using NOW Function in numeric format. Here the result will be in YYYYMMDDHHMMSS.uuuuuu format.
SELECT NOW()+ 0
AS CurrDateTime ;
Output :
CURRDATETIME |
20201126183839 |
Example 3:
In this example, we use the NOW function to set the value of the columns.
First, let’s create a table named DeliveryDetails.
CREATE TABLE Product (
ProductId INT NOT NULL,
ProductName VARCHAR(20) NOT NULL,
Delivered_At TIMESTAMP NOT NULL,
PRIMARY KEY(ProductId)
);
Here, we will use the NOW function to determine when a delivery will be completed. The value in Delivered_At column will be the value given by the NOW function.
INSERT INTO
Product (ProductId, ProductName, Delivered_At)
VALUES
(1010, 'Apple MacBook', NOW());
Now, check the Product table :
SELECT * FROM Product;
Output:
PRODUCTID |
PRODUCTNAME |
DELIVERED_AT |
1010 |
Apple MacBook |
2021-10-01 14:41:15 |
Conclusion
The MySQL NOW()
function is essential for capturing real-time data in your applications. Whether you are creating records with precise timestamps or performing time-sensitive operations, NOW()
ensures that your database accurately reflects the current moment. Use this function wisely, considering server time zone configurations, to achieve accurate and reliable results
Similar Reads
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
MySQL | VERSION( ) Function
The MySQL Version() function is used for returning the current version of the MySQL database. This function uses the utf8 character set. Generally, there is a suffix returned after the version number. The Version() function does not require any parameter to be passed. Syntax: VERSION() Parameters Us
1 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read
MySQL | OLD_PASSWORD Function
The MySQL OLD_PASSWORD function is used for generating a hashed password from a plaintext password string. The OLD_PASSWORD function uses hashing techniques to perform the generation of the hashed password. This function is carried out by the authentication system. This function is used by the MySQL
1 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
TO DAYS() FUNCTION in MySQL
TO DAYS() : TO DAYS() function in MySQL takes the given date and returns a number of days since year 0 corresponding to the given date.TO DAYS() function can only be used with the dates within the Gregorian calendar. Syntax : TO DAYS(date) Parameters: The function can accept only one argument as sho
1 min read
WEEKDAY() Function in MySQL
WEEKDAY() function in MySQL is used to find the weekday value for a given date. If the date is NULL, the WEEKDAY() function will return NULL. Otherwise, it returns index for a date i.e., 0 for Monday, 1 for Tuesday, ⦠6 for Sunday. Syntax : WEEKDAY(date) Parameter : This method accepts one parameter
3 min read
MySQL DATE_SUB() Function
The MySQL DATE_SUB() function subtracts a specified time or date interval from a datetime value. DATE_SUB() Function in MySQLThe DATE_SUB() Function in MySQL allows for various date and time calculations by subtracting time intervals. It is used to subtract a specified time or date interval from a g
2 min read
DAY() Function in MySQL
DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 min read