在MySQL中解析日期 [英] Parse date in MySQL

查看:121
本文介绍了在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.

推荐答案

您可能希望使用 功能.

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.如果格式字符串同时包含日期和时间部分,则STR_TO_DATE()返回一个DATETIME值;如果该字符串仅包含日期或时间部分,则返回DATETIME值.如果从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 aDATETIME 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中解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆