TIME() function in MYSQL
TIME() function in MySQL is used to extracts the time part from a given time/datetime expression. If the expression is not a time or a datetime value, the TIME function will return ’00:00:00′. If expression is NULL, the TIME function will return NULL.
Syntax :
TIME(expression)
Parameter :
This method accepts one parameter.
- expression : The time or datetime value from which we want to extract the time.
Returns :
It returns the time part from a given time/datetime expression.
Example-1 :
Extracting the time using TIME Function from a datetime expression where the datetime is specified in the format of YYYY-MM-DD HH-MM-SS.
SELECT TIME("2019-01-10 08:14:21")as Time ;
Output :
Time |
---|
08:14:21 |
Example-2 :
Extracting the time using TIME Function from a datetime expression where the datetime is specified in the format of HH-MM-SS.
SELECT TIME("18:24:23")as Time ;
Output :
TIME |
---|
18:24:23 |
Example-3 :
Extracting the time using TIME Function from a expression which is not a datetime.
SELECT TIME(NULL) AS TIME;
Output :
TIME |
---|
NULL |
Example-4 :
The TIME function can be used to set value of columns. To demonstrate create a table named DeliveryDetails.
CREATE TABLE DeliveryDetails ( DeliveryId INT AUTO_INCREMENT, ProductId INT NOT NULL, ProductName VARCHAR(20) NOT NULL, Delivered_At TIME NOT NULL, PRIMARY KEY(DeliveryId) );
Here, we will use TIME function when a delivery will be completed. The value in Delivered_At column will be the value given by TIME Function.
INSERT INTO DeliveryDetails(ProductId, ProductName, Delivered_At) VALUES (12345, 'Let Us Java', TIME (NOW()));
Now, checking the DeliveryDetails table :
SELECT * FROM DeliveryDetails;
Output :
DELIVERYID | PRODUCTID | PRODUCTNAME | DELIVERED_AT |
---|---|---|---|
1 | 12345 | Let Us Java | 09:48:34 |