MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL TIME() Function



The MySQL TIME() function is used to extract the time value from a time or datetime expression.

Syntax

TIME(expression)

Parameters

expression Required. Specify a valid time or datetime value from which the time should be extracted. Returns '00:00:00' if the expression is not a valid time or datetime value. Returns NULL if the expression is NULL.

Return Value

Returns the time value of a given time or datetime expression.

Example 1:

The example below shows the usage of TIME() function.

mysql> SELECT TIME('2018-08-18 10:38:42');
Result: '10:38:42'

mysql> SELECT TIME('2018-08-18 10:38:42.000004');
Result: '10:38:42.000004'

mysql> SELECT TIME('10:38:42');
Result: '10:38:42'

mysql> SELECT TIME(NULL);
Result: NULL

mysql> SELECT TIME('Time is 10:38:42');
Result: '00:00:00'

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42.000004
1201.61The DateTime is 2018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34.006789

The statement given below can be used to get the time value of records of column OrderTime:

SELECT *, TIME(OrderTime) AS TIME_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeTIME_Value
1001.582017-08-18 10:38:42.00000410:38:42.000004
1201.612018-03-23 07:14:1607:14:16
1251.782018-09-12 05:25:5605:25:56
501.802019-01-16 11:52:0511:52:05
2001.722020-02-06 09:31:34.00678909:31:34.006789

❮ MySQL Functions