在 MySQL 中解析日期

Parse date in MySQL(在 MySQL 中解析日期)
本文介绍了在 MySQL 中解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何将以下内容转换为用于插入/更新的日期到 MySQL 中的 TIMESTAMPDATE 字段?

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.如果格式字符串包含日期和时间部分,或者 DATETIMESTR_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 中解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)