您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL日期字符串转换成NULL值的异常处理_MySQL

2024/2/18 2:12:57发布7次查看
1,yyyymmdd格式日期转换为null看如下记录,一个能取到值,一个取不到值**
mysql> select date_format(str_to_date(‘20150922 13:01:01’, ‘%y%m%d %h:%m:%s’),’%h:%m’);
+————————————————————————–+
| date_format(str_to_date(‘20150922 13:01:01’, ‘%y%m%d %h:%m:%s’),’%h:%m’) |
+————————————————————————–+
| 13:01 |
+————————————————————————–+
1 row in set (0.00 sec)
mysql> select date_format(str_to_date(‘20150922 12:55:00’, ‘%y%m%d %h:%m:%s’),’%h:%m’);
+————————————————————————–+
| date_format(str_to_date(‘20150922 12:55:00’, ‘%y%m%d %h:%m:%s’),’%h:%m’) |
+————————————————————————–+
| null |
+————————————————————————–+
1 row in set, 1 warning (0.00 sec)
mysql>
2,查看报错信息:mysql> show warnings;
+———+——+————————————————————————+
| level | code | message |
+———+——+————————————————————————+
| warning | 1411 | incorrect datetime value: ‘20150922 12:55:00’ for function str_to_date |
+———+——+————————————————————————+
1 row in set (0.00 sec)
mysql>
报警说是incorrect datetime value: ‘20150922 12:55:00’ for function str_to_date,不正确的日期格式,所以换成比较规范的日期格式
原blog地址:http://blog.csdn.net/mchdba/article/details/48719765,未经过csdn原博客博主mchdba允许,不能转载。
3,换成规范的%y-%m-%d %h:%i:%s试试mysql> select date_format(str_to_date(‘2015-09-22 13:00:01’, ‘%y-%m-%d %h:%i:%s’),’%h:%i’);
+——————————————————————————+
| date_format(str_to_date(‘2015-09-22 13:00:01’, ‘%y-%m-%d %h:%i:%s’),’%h:%i’) |
+——————————————————————————+
| 13:00 |
+——————————————————————————+
1 row in set (0.00 sec)
mysql> select date_format(str_to_date(‘2015-09-22 12:55:00’, ‘%y-%m-%d %h:%i:%s’),’%h:%i’);
+——————————————————————————+
| date_format(str_to_date(‘2015-09-22 12:55:00’, ‘%y-%m-%d %h:%i:%s’),’%h:%i’) |
+——————————————————————————+
| 12:55 |
+——————————————————————————+
1 row in set (0.00 sec)
mysql>
看到在规范的格式下,日期从字符串转换到日期格式,然后截取时分都是能取到值的。
参考官网地址:http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_str-to-date
4,再次确定缩小范围,是%h的问题,将时换成大写就搞定了再看到一个案例,将格式变成2015-09-22 13:00:01表准备格式,可以仍然取不到时分值,如下所示:
mysql> select date_format(str_to_date('2015-09-22 13:00:01', '%y-%m-%d %h:%i:%s'),'%h:%i');+------------------------------------------------------------------------------+| date_format(str_to_date('2015-09-22 13:00:01', '%y-%m-%d %h:%i:%s'),'%h:%i') |+------------------------------------------------------------------------------+| null |+------------------------------------------------------------------------------+1 row in set, 1 warning (0.00 sec)mysql> select date_format(str_to_date('2015-09-22 12:55:00', '%y-%m-%d %h:%i:%s'),'%h:%i');+------------------------------------------------------------------------------+| date_format(str_to_date('2015-09-22 12:55:00', '%y-%m-%d %h:%i:%s'),'%h:%i') |+------------------------------------------------------------------------------+| 12:55 |+------------------------------------------------------------------------------+1 row in set (0.00 sec)mysql>
那么问题在哪里呢?只能再次去官网找答案,找到http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_get-format
看到终极标准格式为:’%y-%m-%d %h.%i.%s’,换成这个试试,看到范例中的差异在哪里了吗?就在于%h和%h的区别啊,如下所示:
mysql> select date_format(str_to_date('20150922 13:00:01', '%y%m%d %h:%i:%s'),'%h:%i');+--------------------------------------------------------------------------+| date_format(str_to_date('20150922 13:00:01', '%y%m%d %h:%i:%s'),'%h:%i') |+--------------------------------------------------------------------------+| null |+--------------------------------------------------------------------------+1 row in set, 1 warning (0.00 sec)mysql> select date_format(str_to_date('20150922 12:55:00', '%y%m%d %h:%i:%s'),'%h:%i');+--------------------------------------------------------------------------+| date_format(str_to_date('20150922 12:55:00', '%y%m%d %h:%i:%s'),'%h:%i') |+--------------------------------------------------------------------------+| 12:55 |+--------------------------------------------------------------------------+1 row in set (0.00 sec)mysql>
然后标准的日期格式为:%y%m%d %h:%i:%s’或者%y-%m-%d %h:%i:%s’ 都可以的,如下所示:
select date_format(str_to_date(‘20150922 13:00:01’, ‘%y%m%d %h:%i:%s’),’%h:%i’);
select date_format(str_to_date(‘20150922 12:55:00’, ‘%y%m%d %h:%i:%s’),’%h:%i’);
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product