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

# str_to_date

> 按照指定格式将字符串转换为 DATETIME 类型的值。

按照 `format` 指定的格式将 `str` 转换为 DATETIME 类型的值。如果转换结果有误，则返回 NULL。该函数与 [date\_format](/documents/cn/4.2/sql-reference/sql-functions/date-time-functions/date_format.md) 函数功能相反。

## 语法

```Haskell theme={null}
DATETIME STR_TO_DATE(VARCHAR str, VARCHAR format)
```

## 参数说明

`str`: 要转换的时间字符串，支持的数据类型为 VARCHAR。

`format`: 指定的时间格式，支持的数据类型为 VARCHAR。支持的时间格式与 [date\_format](/documents/cn/4.2/sql-reference/sql-functions/date-time-functions/date_format.md) 函数一致。

## 返回值说明

返回 DATETIME 类型的值。如果 `format` 指定的格式为日期，则返回 DATE 类型的值。

如果 `str` 或 `format` 为 NULL，则返回 NULL。

## 示例

示例一：将输入值转换为 DATETIME 类型的日期时间。

```Plain theme={null}
select str_to_date('2014-12-21 12:34:56', '%Y-%m-%d %H:%i:%s');
+---------------------------------------------------------+
| str_to_date('2014-12-21 12:34:56', '%Y-%m-%d %H:%i:%s') |
+---------------------------------------------------------+
| 2014-12-21 12:34:56                                     |
+---------------------------------------------------------+
```

示例二：将输入值转换为 DATE 类型的日期。

```Plain theme={null}
SELECT STR_TO_DATE('20141221','%Y%m%d');
+-----------------------------------+
| str_to_date('20141221', '%Y%m%d') |
+-----------------------------------+
| 2014-12-21                        |
+-----------------------------------+
```

示例三：将 2004 年第 42 周的星期一 (`200442 Monday`) 转换为日期。

```Plain theme={null}
select str_to_date('200442 Monday', '%X%V %W');
+-----------------------------------------+
| str_to_date('200442 Monday', '%X%V %W') |
+-----------------------------------------+
| 2004-10-18                              |
+-----------------------------------------+
```


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