NOW() function in MYSQL
Last Updated :
11 Jan, 2021
NOW() :
This function in MySQL is used to check the current date and time value. The return type for 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 :
NOW()
Parameter :
This method does not accept any parameter.
Returns :
It returns the current date and time value.
Example-1 :
Finding the current date and time using NOW Function. Here we will get the result in ‘YYYY-MM-DD HH:MM:SS’ format.
SELECT NOW()
as CurrDateTime ;
Output :
CURRDATETIME |
2020-11-26 18:37:42 |
Example-2 :
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 :
We can also use The NOW function to set value of columns. To demonstrate 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 NOW function when a delivery will be completed. The value in Delivered_At column will be the value given by NOW Function.
INSERT INTO
Product (ProductId, ProductName, Delivered_At)
VALUES
(1010, 'Apple MacBook', NOW());
Now, checking the Product table :
SELECT * FROM Product;
Output :
PRODUCTID |
PRODUCTNAME |
DELIVERED_AT |
1010 |
Apple MacBook |
2021-10-01 14:41:15 |
Share your thoughts in the comments
Please Login to comment...