DATE_FORMAT_MYSQL

date_format_mysql(expr, fmt)

Description

The DATE_FORMAT_MYSQL function is used to convert different types of timestamps (including datetime, timestamp_ltz, string, etc.) into a string format according to the specified format. This function is compatible with MySQL database date and time formatting rules.

Parameter Description

  • expr: The input timestamp, which can be in datetime, timestamp_ltz, or string format.
  • fmt: A string describing the date and time format. For specific formatting options, please refer to the MySQL official documentation: DATE_FORMAT()

Formatting options description:

OptionDescription
%aAbbreviated weekday name (e.g., Sun to Sat)
%bAbbreviated month name (e.g., Jan to Dec)
%cMonth number (00 to 12)
%DDay of the month with English suffix (e.g., 1st, 2nd, 3rd, ...)
%dDay of the month, numeric (00 to 31)
%eDay of the month, numeric (0 to 31)
%fMicroseconds (000000 to 999999)
%HHour (00 to 23)
%hHour (01 to 12)
%IHour (01 to 12)
%iMinutes (00 to 59)
%jDay of the year (001 to 366)
%kHour (0 to 23)
%lHour (1 to 12)
%MMonth name (January to December)
%mMonth number (00 to 12)
%pAM or PM
%SSeconds (00 to 59)
%sSeconds (00 to 59)
%T24-hour time (hh:mm:ss)
%vWeek number (01 to 53), with Monday as the first day of the week; WEEK() mode 3; used with %x
%WWeekday name (Sunday to Saturday)
%wDay of the week (0 for Sunday, 6 for Saturday)
%xYear for the week where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric, two digits
%%A literal % character

Return Result

Returns the date and time string converted according to the specified format.

Example

  1. Convert the current time to a string format with seconds:
SELECT date_format_mysql(now(), '%Y-%m-%d %H:%i:%s');
  1. Convert the timestamp to a format that only includes hours and minutes:
SELECT date_format_mysql(timestamp '2023-03-22 13:45:00', '%H:%i');

Through the above examples, you can see the application of the DATE_FORMAT_MYSQL function in different scenarios. You can adjust the fmt parameter as needed to get your desired date and time format.