メインコンテンツまでスキップ
に最終更新

DATE_FORMAT

説明​

DATE_FORMAT 関数は、指定した書式文字列(format)に従って日付または時刻の値を文字列に変換します。DATE 型(日付のみ)と DATETIME 型(日付と時刻)の書式設定をサポートし、指定した書式に従う文字列を返します。

この関数の動作は、MySQL の date_format 関数と同じです。

構文​

DATE_FORMAT(<date_or_time_expr>, <format>)

パラメータ​

パラメータ説明
<date_or_time_expr>有効な日付値。DATETIME 型または DATE 型をサポートします。具体的な日時および日付の書式については、日時の変換と日付の変換を参照してください。
<format>日付と時刻の出力書式を指定します。型は varchar です。

サポートされる書式指定子は次のとおりです。

書式指定子説明
%a曜日の省略名(3 文字)
%b月の省略名(3 文字)
%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
%r12 時間形式の時刻(hh:mm:ss の後に AM または PM)
%S秒(00-59)
%s秒(00-59)
%T24 時間形式の時刻(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 --corresponds to standard format specifier:%Y%m%d
yyyy-MM-dd --corresponds to standard format specifier:%Y-%m-%d
yyyy-MM-dd HH:mm:ss --corresponds to standard format specifier:%Y-%m-%d %H:%i:%s

戻り値​

書式設定された日付文字列を返します。型は Varchar です。

特殊なケース:

  • 書式が NULL の場合は NULL を返します。
  • いずれかのパラメータが NULL の場合は NULL を返します。
  • 入力値の長さが 128 文字を超える場合はエラーを返します。
  • 出力長が 102 を超える場合はエラーを返します。

例​

-- Output weekday name, month name, and 4-digit year
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 |
+------------------------------------------------------------------+

-- Output time in 24-hour format (hour:minute:second)
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 |
+------------------------------------------------+

-- Combine multiple format specifiers and plain characters
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 (year) used with %V (week number), where Sunday is the first day of the week
select date_format('1999-01-01 00:00:00', '%X-%V');

+---------------------------------------------+
| date_format('1999-01-01 00:00:00', '%X-%V') |
+---------------------------------------------+
| 1998-52 |
+---------------------------------------------+

-- Output the % character (escaped with %%)
select date_format(cast('2006-06-01' as date), '%%%d/%m');

+----------------------------------------------------+
| date_format(cast('2006-06-01' as date), '%%%d/%m') |
+----------------------------------------------------+
| %01/06 |
+----------------------------------------------------+

-- Special format 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 |
+-----------------------------------------------------------+

-- String that does not reference any time information
select date_format('2023-12-31 23:59:59', 'ghg');
+-------------------------------------------+
| date_format('2023-12-31 23:59:59', 'ghg') |
+-------------------------------------------+
| ghg |
+-------------------------------------------+

-- Special format yyyyMMdd
select date_format('2023-12-31 23:59:59', 'yyyyMMdd');
+------------------------------------------------+
| date_format('2023-12-31 23:59:59', 'yyyyMMdd') |
+------------------------------------------------+
| 20231231 |
+------------------------------------------------+

-- Special format 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 |
+--------------------------------------------------+

-- Parameter is null
mysql> select date_format(NULL, '%Y-%m-%d');
+-------------------------------+
| date_format(NULL, '%Y-%m-%d') |
+-------------------------------+
| NULL |
+-------------------------------+

---Returns error if the result string exceeds the function's maximum length limit.
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

---output length over 102 size,throw error
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