MariaDB TIME_FORMAT() Function
The MariaDB TIME_FORMAT() function formats a time or datetime value as specified by a format mask. This function only formats the hours, minutes, seconds, and microseconds found in a time or datetime value.
Syntax
TIME_FORMAT(datetime, format_mask)
Parameters
datetime |
Required. Specify the time or datetime value to format. | ||||||||||||||||||||||
format_mask |
Required. Specify the format to apply to the datetime. The following is a list of options for this parameter. It can be used in many combinations.
|
Return Value
Returns the formatted time or datetime value as specified by a format mask.
Example 1:
The example below shows the usage of TIME_FORMAT() function.
SELECT TIME_FORMAT('16:38:42', '%H %i %s'); Result: '16 38 42' SELECT TIME_FORMAT('16:38:42', '%h:%i:%s %p'); Result: '04:38:42 PM' SELECT TIME_FORMAT('16:38:42', '%h:%i %p'); Result: '04:38 PM' SELECT TIME_FORMAT('16:38:42.000123', '%r'); Result: '04:38:42 PM' SELECT TIME_FORMAT('16:38:42.000123', '%T'); Result: '16:38:42' SELECT TIME_FORMAT('16:38:42.000123', '%f'); Result: '000123' SELECT TIME_FORMAT('2018-08-18 16:38:42.000004', '%h:%i:%s.%f'); Result: '04:38:42.000004'
Example 2:
Consider a database table called Orders with the following records:
OrderQuantity | Price | OrderTime |
---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 |
120 | 1.61 | 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 |
In the query below, the TIME_FORMAT() function is used to format the datetime value of OrderTime column:
SELECT *, TIME_FORMAT(OrderTime, '%h:%i %p') AS TIME_FORMAT_Value FROM Orders;
This will produce a result similar to:
OrderQuantity | Price | OrderTime | TIME_FORMAT_Value |
---|---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 | 10:38 AM |
120 | 1.61 | 2018-03-23 07:14:16 | 07:14 AM |
125 | 1.78 | 2018-09-12 05:25:56 | 05:25 AM |
50 | 1.80 | 2019-01-16 11:52:05 | 11:52 AM |
200 | 1.72 | 2020-02-06 09:31:34 | 09:31 AM |
❮ MariaDB Functions