MySQL NOW() function
In this context, we will learn how we can use the MySQL NOW() function with proper syntax and good examples.
Introduction of MySQL NOW() function
Basically, in MySQL, this function is used to check the current date and time value. The return type for the NOW() function is 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 of the MySQL NOW() function
The syntax of the MySQL NOW() function is given as follows:
NOW()
Parameters or arguments used in MySQL NOW() function:
There is no parameter accepted by the NOW() function in MySQL.
Returns:
It will return the current date and time value.
Application used for NOW() function:
The NOW() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL NOW() function:
Now we will look into some MySQL NOW() function examples and will explore how we can use the NOW function in MySQL.
Example-1 :
Using NOW Function, we will find the current date and time in this example. Here we will get the result in 'YYYY-MM-DD HH:MM: SS' format in MySQL.
SELECT NOW()
as CurrDateTime ;
Output:
CURRDATETIME
2021-10-17 14:37:48
1 row in set (0.00 sec)
Example-2:
In this example, we will get the current date and time using NOW Function in the numeric format in MySQL. Here the result will be in YYYYMMDDHHMMSS.uuuuuu format.
SELECT NOW()+ 0 as CurrDateTime ;
Output:
CURRDATETIME
20211221123839
1 row in set (0.00 sec)
Example-3 :
We can also use The NOW function to set the value of columns. To demonstrate, create a table named Commoditydetails.
CREATE TABLE Commodity (
CommodityId INT NOT NULL,
CommodityName VARCHAR(20) NOT NULL,
Delivered_At TIMESTAMP NOT NULL,
PRIMARY KEY(CommodityId)
);
Here, we will use the NOW function when a delivery is completed. The value in the Delivered_At column will be the value given by NOW Function.
INSERT INTO
Commodity (CommodityId, CommodityName, Delivered_At)
VALUES
(1010, 'Apple MacBook,' NOW());
Now, we will check the Commodity table using the following SELECT statement:
SELECT * FROM Commodity;
Output:
COMMODITYID | COMMODITYNAME | DELIVERED_AT |
1010 | Apple MacBook | 2020-10-11 14:41:15 |
Application of MySQL NOW() function:
This function is used to check the current date and time value.
Summary:
In the above context, we have learned how to use the NOW() function in MySQL to check the current date and time value.