Parse date in MySQL(在 MySQL 中解析日期)
问题描述
如何将以下内容转换为用于插入/更新的日期到 MySQL 中的 TIMESTAMP
或 DATE
字段?
How to convert the following into date for insertion/update into a TIMESTAMP
or DATE
field in MySQL?
'15-Dec-09'
DATE_FORMAT()
用于格式化日期,反之则不然.
DATE_FORMAT()
is used to format date, but not the other way around.
推荐答案
您可能想要使用 STR_TO_DATE()
函数.它是 DATE_FORMAT()
函数.
You may want to use the STR_TO_DATE()
function. It's the inverse of the DATE_FORMAT()
function.
STR_TO_DATE(str,format)
这是 DATE_FORMAT()
函数的反函数.它需要一个字符串 str
和一个格式字符串 format
.如果格式字符串包含日期和时间部分,或者 DATE
或 TIME
,STR_TO_DATE()
返回一个 DATETIME
值> value 如果字符串只包含日期或时间部分.如果从 str
中提取的日期、时间或日期时间值不合法,STR_TO_DATE()
将返回 NULL
并产生警告.
This is the inverse of the DATE_FORMAT()
function. It takes a string str
and a format string format
. STR_TO_DATE()
returns a DATETIME
value if the format string contains both date and time parts, or a DATE
or TIME
value if the string contains only date or time parts. If the date, time, or datetime value extracted from str
is illegal, STR_TO_DATE()
returns NULL
and produces a warning.
示例:
SELECT STR_TO_DATE('15-Dec-09', '%d-%b-%y') AS date;
+------------+
| date |
+------------+
| 2009-12-15 |
+------------+
1 row in set (0.00 sec)
这篇关于在 MySQL 中解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 中解析日期


- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01