跳到主要内容
版本:Latest-3.2

to_tera_date

功能

根据指定的格式解析输入的时间日期字符串,并转换为日期。

语法

DATE to_tera_date(VARCHAR str, VARCHAR format)

参数说明

  • str:待转换的时间表达式,取值必须是 VARCHAR 类型。

  • format:日期格式,用于解析和转换输入的字符串。format 必须与 str 匹配。否则返回 NULL。如果 format 为无效值,返回报错。

    有关格式中各元素的含义介绍,参见下表。

    ElementDescription
    [ \r \n \t - / , . ;]标点类符号,忽略。
    dd代表日期 (1 - 31)
    hh代表小时,采用十二小时制 (1 - 12)
    hh24代表小时,采用二十四小时制 (0 - 23)
    mi代表分钟 (0 - 59)
    mm代表月份 (01 - 12)
    ss代表秒 (0 - 59)
    yyyy代表四位数的年份。
    yy代表两位数的年份。
    am代表十二小时制里的上午。
    pm代表十二小时制里的下午。

返回值说明

返回一个日期。如果输入值为 NULL,则返回 NULL。

示例

select to_tera_date("1988/04/08","yyyy/mm/dd");
+------------------------------------------+
| to_tera_date('1988/04/08', 'yyyy/mm/dd') |
+------------------------------------------+
| 1988-04-08 |
+------------------------------------------+

select to_tera_date("04-08-1988","mm-dd-yyyy");
+------------------------------------------+
| to_tera_date('04-08-1988', 'mm-dd-yyyy') |
+------------------------------------------+
| 1988-04-08 |
+------------------------------------------+

select to_tera_date(";198804:08",";yyyymm:dd");
+------------------------------------------+
| to_tera_date(';198804:08', ';yyyymm:dd') |
+------------------------------------------+
| 1988-04-08 |
+------------------------------------------+

select to_tera_date("2020-02-02 00:00:00", "yyyy-mm-dd");
+---------------------------------------------------+
| to_tera_date('2020-02-02 00:00:00', 'yyyy-mm-dd') |
+---------------------------------------------------+
| 2020-02-02 |
+---------------------------------------------------+

-- 输入值为年份,不包含日期,则默认返回当年第一天。
select to_tera_date("1988","yyyy");
+------------------------------+
| to_tera_date('1988', 'yyyy') |
+------------------------------+
| 1988-01-01 |
+------------------------------+

关键字

TO_TERA_DATE