首页 > 数据库技术 > 详细

mysql str_to_date字符串转换为日期

时间:2015-07-07 00:40:31      阅读:287      评论:0      收藏:0      [点我收藏+]

mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期。

示例:分隔符一致,年月日要一致

 

select str_to_date(‘2008-4-2 15:3:28‘,‘%Y-%m-%d %H:%i:%s‘);

select str_to_date(‘2008-08-09 08:9:30‘, ‘%Y-%m-%d %h:%i:%s‘);

 

对于这个已经理解,但是为何查询字段时使用这种方法查询出来的数据为null????

 

刚发现的问题,尝试在client上查询,发现可以出现数据,但是在SQLyog上却是null

 

mysql> SELECT STR_TO_DATE(startDate,‘%Y年%m月%d日‘) FROM caselibrarycommon;
+---------------------------------------+
| STR_TO_DATE(startDate,‘%Y年%m月%d日‘) |
+---------------------------------------+
| 2015-06-16 |
| 2015-06-05 |
| 2015-06-06 |
| 2015-06-06 |
| 2015-06-06 |
| 2015-06-06 |
| 2015-06-07 |
| 2015-06-09 |
| 2015-06-08 |
| 2015-06-10 |
| 2015-06-11 |
| 2015-06-11 |
| 2015-06-11 |
| 2015-06-12 |

 

技术分享

 

这是什么原因?

 


mysql> SELECT STR_TO_DATE(startDate,‘%Y年%m月%d日‘) FROM caselibrarycommon
-> WHERE STR_TO_DATE(startDate,‘%Y年%m月%d日‘) BETWEEN STR_TO_DATE(‘2015年6
‘> 月5日‘,‘%Y年%m月%d日‘) AND STR_TO_DATE(‘2015年6月20日‘,‘%Y年%m月%d日‘);
+---------------------------------------+
| STR_TO_DATE(startDate,‘%Y年%m月%d日‘) |
+---------------------------------------+
| 2015-06-16 |
| 2015-06-05 |
| 2015-06-06 |
| 2015-06-06 |
| 2015-06-06 |
| 2015-06-06 |
| 2015-06-07 |
| 2015-06-09 |
| 2015-06-08 |
| 2015-06-10 |
| 2015-06-11 |
| 2015-06-11 |
| 2015-06-11 |
| 2015-06-12 |
| 2015-06-16 |
| 2015-06-16 |
| 2015-06-17 |
| 2015-06-17 |
| 2015-06-17 |
| 2015-06-18 |
+---------------------------------------+
20 rows in set (0.00 sec)

 

select str_to_date(detectResult.`rcvDetectTime`,‘%Y-%m-%d %H:%i:%s‘) from detectResult where str_to_date(detectResult.`rcvDetectTime`,‘%Y-%m-%d %H:%i:%s‘)>=‘2010-11-22 14:49:52‘ and str_to_date(detectResult.`rcvDetectTime`,‘%Y-%m-%d %H:%i:%s‘)<=‘2010-11-22 15:27:52‘

 

给自己点个赞,么么哒(づ ̄ 3 ̄)づ

mysql str_to_date字符串转换为日期

原文:http://www.cnblogs.com/LuoXiaoTing604404828/p/4625729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!