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

to_tera_timestamp

功能

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

语法

DATETIME to_tera_timestamp(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代表十二小时制里的下午。

示例

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

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

select to_tera_timestamp("04.1988,08","mm.yyyy,dd");
+-----------------------------------------------+
| to_tera_timestamp('04.1988,08', 'mm.yyyy,dd') |
+-----------------------------------------------+
| 1988-04-08 00:00:00 |
+-----------------------------------------------+

select to_tera_timestamp("1988/04/08 2","yyyy/mm/dd hh");
+----------------------------------------------------+
| to_tera_timestamp('1988/04/08 2', 'yyyy/mm/dd hh') |
+----------------------------------------------------+
| 1988-04-08 02:00:00 |
+----------------------------------------------------+

select to_tera_timestamp("1988/04/08 14","yyyy/mm/dd hh24");
+-------------------------------------------------------+
| to_tera_timestamp('1988/04/08 14', 'yyyy/mm/dd hh24') |
+-------------------------------------------------------+
| 1988-04-08 14:00:00 |
+-------------------------------------------------------+

select to_tera_timestamp("1988/04/08 14:15","yyyy/mm/dd hh24:mi");
+-------------------------------------------------------------+
| to_tera_timestamp('1988/04/08 14:15', 'yyyy/mm/dd hh24:mi') |
+-------------------------------------------------------------+
| 1988-04-08 14:15:00 |
+-------------------------------------------------------------+

select to_tera_timestamp("1988/04/08 2:3:4","yyyy/mm/dd hh24:mi:ss");
+----------------------------------------------------------------+
| to_tera_timestamp('1988/04/08 2:3:4', 'yyyy/mm/dd hh24:mi:ss') |
+----------------------------------------------------------------+
| 1988-04-08 02:03:04 |
+----------------------------------------------------------------+

select to_tera_timestamp("1988/04/08 02 am:3:4","yyyy/mm/dd hh am:mi:ss");
+---------------------------------------------------------------------+
| to_tera_timestamp('1988/04/08 02 am:3:4', 'yyyy/mm/dd hh am:mi:ss') |
+---------------------------------------------------------------------+
| 1988-04-08 02:03:04 |
+---------------------------------------------------------------------+

关键字

TO_TERA_TIMESTAMP