MariaDB TIME_FORMAT() Function

In MariaDB, TIME_FORMAT() is a built-in function that formats a given time according to a given format.

MariaDB TIME_FORMAT() Syntax

This is the syntax of the MariaDB TIME_FORMAT() function:

TIME_FORMAT(time, format)

Parameters

time

Required. The time to format.

format

Required. Formatting pattern string.

The following table organizes the formatting symbols available in format:

Placeholder Description
%f microseconds (000000.. 999999)
%H hours (00.. 23)
%h hours (01.. 12)
%I hours (01.. 12)
%i minutes (00.. 59)
%k hours (0.. 23)
%l hours (1.. 12)
%p AM or PM
%r twelve-hour time ( hh:mm:ss followed by AM or PM)
%S seconds (00.. 59)
%s seconds (00.. 59)
%T twenty-four hour time ( hh:mm:ss)
%% escape%

If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'TIME_FORMAT'.

Return value

The MariaDB TIME_FORMAT() function formats the time according to the specified format and returns the formatted string.

The TIME_FORMAT() function will return NULL if any of the arguments are NULL.

MariaDB TIME_FORMAT() Examples

The following statement shows the basic usage of the MariaDB TIME_FORMAT() function:

SELECT
    TIME_FORMAT("19:30:10", "%H %i %s"),
    TIME_FORMAT("19:30:10", "%h %i %s %p"),
    TIME_FORMAT("19:30:10", "%r"),
    TIME_FORMAT("19:30:10", "%T"),
    TIME_FORMAT("19:30:10", "%H %i %s")\G

Output:

   TIME_FORMAT("19:30:10", "%H %i %s"): 19 30 10
TIME_FORMAT("19:30:10", "%h %i %s %p"): 07 30 10 PM
         TIME_FORMAT("19:30:10", "%r"): 07:30:10 PM
         TIME_FORMAT("19:30:10", "%T"): 19:30:10
   TIME_FORMAT("19:30:10", "%H %i %s"): 19 30 10

Here it is again, but this time we provide a more verbose format string:

SELECT TIME_FORMAT(
    '10:30:45',
    '%H hours, %i minutes, and %S seconds'
    )
AS Result;

Output:

+--------------------------------------+
| Result                               |
+--------------------------------------+
| 10 hours, 30 minutes, and 45 seconds |
+--------------------------------------+

Conclusion

In MariaDB, TIME_FORMAT() is a built-in function that formats a given time according to a given format.