PostgreSQL TIME() Function
The PostgreSQL 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.
SELECT TIME('2018-08-18 10:38:42'); Result: '10:38:42' SELECT TIME('2018-08-18 10:38:42.000004'); Result: '10:38:42.000004' SELECT TIME('10:38:42'); Result: '10:38:42' SELECT TIME(NULL); Result: NULL SELECT TIME('Time is 10:38:42'); Result: '00:00:00'
Example 2:
Consider a database table called Orders with the following records:
OrderQuantity | Price | OrderTime |
---|---|---|
100 | 1.58 | 2017-08-18 10:38:42.000004 |
120 | 1.61 | The DateTime is 2018-03-23 07:14:16 |
125 | 1.78 | 2018-09-12 05:25:56 |
50 | 1.80 | 2019-01-16 11:52:05 |
200 | 1.72 | 2020-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:
OrderQuantity | Price | OrderTime | TIME_Value |
---|---|---|---|
100 | 1.58 | 2017-08-18 10:38:42.000004 | 10:38:42.000004 |
120 | 1.61 | 2018-03-23 07:14:16 | 07:14:16 |
125 | 1.78 | 2018-09-12 05:25:56 | 05:25:56 |
50 | 1.80 | 2019-01-16 11:52:05 | 11:52:05 |
200 | 1.72 | 2020-02-06 09:31:34.006789 | 09:31:34.006789 |
❮ PostgreSQL Functions