> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# to_tera_date

> 根据指定格式解析时间日期字符串并转换为DATE值。

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

## 语法

```Haskell theme={null}
DATE to_tera_date(VARCHAR str, VARCHAR format)
```

## 参数说明

* `str`：待转换的时间表达式，取值必须是 VARCHAR 类型。

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

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

  | **Element** | **Description** |
  | - | - |
  | \[ \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。

## 示例

```SQL theme={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


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.