MySQL: TIME_FORMAT Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL TIME_FORMAT Function

In this guide, we will explain how to use the MySQL TIME_FORMAT function with syntax and examples.

Description

The MySQL TIME_FORMAT function formats a time as specified by a format mask.

Syntax

The syntax for the TIME_FORMAT function in MySQL is:

TIME_FORMAT( time, format_mask )

Parameters or Arguments

time

The time to format.format_mask

The format to apply to time. The following is a list of options for the format_mask parameter. These parameters can be used in many combinations.

ValueDescription
%fMicroseconds (000000 to 999999)
%f is available starting in MySQL 4.1.1
%HHour (00 to 23 generally, but can be higher)
%hHour (00 to 12)
%IHour (00 to 12)
%iMinutes (00 to 59)
%pAM or PM
%rTime in 12 hour AM or PM format (hh:mm:ss AM/PM)
%SSeconds (00 to 59)
%sSeconds (00 to 59)
%TTime in 24 hour format (hh:mm:ss)

Note

  • The TIME_FORMAT function only formats the hours, minutes, seconds, and microseconds found in a time value.
  • See also the DATE_FORMAT function.

Applies To

The TIME_FORMAT function can be used in the following versions :

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let’s look at some TIME_FORMAT function examples and explore how to use the TIME_FORMAT function.

For example:

mysql> SELECT TIME_FORMAT('15:02:28', '%H %i %s');
Result: '15 02 28'

mysql> SELECT TIME_FORMAT('15:02:28', '%h:%i:%s %p');
Result: '03:02:28 PM'

mysql> SELECT TIME_FORMAT('15:02:28', '%h:%i%p');
Result: '03:02PM'

mysql> SELECT TIME_FORMAT('17:42:03.000001', '%r');
Result: '05:42:03 PM'

mysql> SELECT TIME_FORMAT('17:42:03.000001', '%T');
Result: '17:42:03'

mysql> SELECT TIME_FORMAT('07:42:03.000001', '%f');
Result: '000001'

Next Topic : Click Here

This Post Has One Comment

Leave a Reply