跳到主要内容

DATE_FORMAT

描述

DATE_FORMAT 函数用于将日期或时间值按照指定的格式字符串(format)转换为字符串。支持对 DATE(仅日期)和 DATETIME(日期和时间)类型进行格式化,输出结果为符合格式要求的字符串。

该函数与 mysql 中的 date_format 函数 行为一致

语法

DATE_FORMAT(<date_or_time_expr>, <format>)

参数

参数说明
<date_or_time_expr>合法的日期值,支持为 datetime 或者 date 类型,具体 datetime 和 date 格式请查看 datetime 的转换date 的转换)
<format>规定日期/时间的输出格式,为 varchar 类型

支持的 format 格式:

格式符描述
%a三字母缩写星期名
%b三字母缩写月名
%c月,数值 (0-12)
%D带有英文后缀的月中的天 (0th, 1st, 2nd, 3rd, …)
%d月的天,数值 (00-31)
%e月的天,数值 (0-31)
%f微秒 (000000-999999)
%H小时 (00-23)
%h小时 (01-12)
%I小时 (01-12)
%i分钟,数值 (00-59)
%j年的天 (001-366)
%k小时 (0-23)
%l小时 (1-12)
%M月名
%m月,数值 (00-12)
%pAM 或 PM
%r时间,12-小时(hh:mm:ss, 后跟 AM 或 PM)
%S秒 (00-59)
%s秒 (00-59)
%T时间,24-小时 (hh:mm:ss)
%U周 (00-53) 星期日是一周的第一天,week,模式 0
%u周 (00-53) 星期一是一周的第一天,week,模式 1
%V周 (01-53) 星期日是一周的第一天,week,模式 2,与 %X 使用
%v周 (01-53) 星期一是一周的第一天,week,模式 3,与 %x 使用
%W周中日的名称 (Sunday-Saturday)
%w周的天(0=星期日,6=星期六)
%X年,其中的星期日是周的第一天,4 位,与 %V 使用
%x年,其中的星期一是周的第一天,4 位,与 %v 使用
%Y年,4 位
%y年,2 位
%%用于表示 %
%x对于任何未出现在上列的 x,表示 x 本身

还可以使用三种特殊格式:

yyyyMMdd --对应标准格式符:%Y%m%d
yyyy-MM-dd --对应标准格式符:%Y-%m-%d
yyyy-MM-dd HH:mm:ss --对应标准格式符:%Y-%m-%d %H:%i:%s

返回值

格式化后的日期字符串,类型为 Varchar。

特殊情况:

  • format 为 NULL 返回 NULL。
  • 任一参数为 NULL 返回 NULL。
  • 如果输入字符超过 128 字符长度,返回错误
  • 如果返回结果字符串长度超过 102 ,返回错误

举例

-- 输出星期名、月名、4位年份
select date_format(cast('2009-10-04 22:23:00' as datetime), '%W %M %Y');
+------------------------------------------------------------------+
| date_format(cast('2009-10-04 22:23:00' as datetime), '%W %M %Y') |
+------------------------------------------------------------------+
| Sunday October 2009 |
+------------------------------------------------------------------+

-- 输出24小时制时间(时:分:秒)
select date_format('2007-10-04 22:23:00', '%H:%i:%s');
+------------------------------------------------+
| date_format('2007-10-04 22:23:00', '%H:%i:%s') |
+------------------------------------------------+
| 22:23:00 |
+------------------------------------------------+

-- 组合多种格式符和普通字符
select date_format('1900-10-04 22:23:00', 'Day: %D, Year: %y, Month: %b, DayOfYear: %j');
+-----------------------------------------------------------------------------------+
| date_format('1900-10-04 22:23:00', 'Day: %D, Year: %y, Month: %b, DayOfYear: %j') |
+-----------------------------------------------------------------------------------+
| Day: 4th, Year: 00, Month: Oct, DayOfYear: 277 |
+-----------------------------------------------------------------------------------+

-- %X(年份)与 %V(周数)搭配(星期日为周首)
select date_format('1999-01-01 00:00:00', '%X-%V');
+---------------------------------------------+
| date_format('1999-01-01 00:00:00', '%X-%V') |
+---------------------------------------------+
| 1998-52 |
+---------------------------------------------+

-- 输出 % 字符(需用 %% 转义)
select date_format(cast('2006-06-01' as date), '%%%d/%m');
+----------------------------------------------------+
| date_format(cast('2006-06-01' as date), '%%%d/%m') |
+----------------------------------------------------+
| %01/06 |
+----------------------------------------------------+

---特殊的格式 yyyy-MM-dd HH:mm:ss
select date_format('2023-12-31 23:59:59', 'yyyy-MM-dd HH:mm:ss');
+-----------------------------------------------------------+
| date_format('2023-12-31 23:59:59', 'yyyy-MM-dd HH:mm:ss') |
+-----------------------------------------------------------+
| 2023-12-31 23:59:59 |
+-----------------------------------------------------------+

---匹配字符串未引用任何时间的结果
select date_format('2023-12-31 23:59:59', 'ghg');
+-------------------------------------------+
| date_format('2023-12-31 23:59:59', 'ghg') |
+-------------------------------------------+
| ghg |
+-------------------------------------------+

---特殊格式 yyyyMMdd
select date_format('2023-12-31 23:59:59', 'yyyyMMdd');
+------------------------------------------------+
| date_format('2023-12-31 23:59:59', 'yyyyMMdd') |
+------------------------------------------------+
| 20231231 |
+------------------------------------------------+

---特殊格式 yyyy-MM-dd
select date_format('2023-12-31 23:59:59', 'yyyy-MM-dd');
+--------------------------------------------------+
| date_format('2023-12-31 23:59:59', 'yyyy-MM-dd') |
+--------------------------------------------------+
| 2023-12-31 |
+--------------------------------------------------+

---参数为 null
mysql> select date_format(NULL, '%Y-%m-%d');
+-------------------------------+
| date_format(NULL, '%Y-%m-%d') |
+-------------------------------+
| NULL |
+-------------------------------+

---超出函数输入字符串长度范围,返回错误
mysql> select date_format('2022-01-12',repeat('a',129));
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]Operation date_format of invalid or oversized format is invalid

---输出结果超过 102 字符串长度,报错
mysql> select date_format('2022-11-13 10:12:12',repeat('%h',52));
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[INVALID_ARGUMENT]Operation date_format of 142335809712816128,%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h%h is invalid