Updated on 2026-11-06 GMT+08:00

date_format

The date_format function is used to convert a date to a string in the format specified by format.

Syntax

date_format(string date, string format)

Parameters

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

date

Yes

DATE

or

STRING

The date to be converted.

Formats:

  • yyyy-mm-dd
  • yyyy-mm-dd hh:mi:ss
  • yyyy-mm-dd hh:mi:ss.ff3

format

Yes

STRING

The target format.

The format is a combination of time units representing year, month, day, hour, minute, and second, along with any characters.

  • yyyy indicates year.
  • MM indicates month.
  • dd indicates day.
  • HH indicates hour in 24-hour format.
  • hh indicates hour in 12-hour format.
  • mm indicates minute.
  • ss indicates second.

Return Values

Returns a date of type STRING according to the specified format.

  • If date is not of type DATE or STRING, an error is returned with the message "data type mismatch".
  • If date is of type DATE or STRING but does not conform to the input format for date values, NULL is returned.
  • If the date value is NULL, NULL is returned.
  • If the format value is NULL, NULL is returned.

Example Code

Returns 2023-08-14.

select date_format('2023-08-14','yyyy-MM-dd');

Returns 2023-08.

select date_format('2023-08-14','yyyy-MM')

Returns 20230814.

select date_format('2023-08-14','yyyyMMdd')