如果仅使用时间组件调用,MySQL的STR_TO_DATE()将返回NULL

时间:2016-08-07 18:17:28

标签: mysql

我发现MySQL的功能STR_TO_DATE()有些奇怪。它似乎无法解析时间,如下:

它适用于完整的字符串:

mysql> SELECT STR_TO_DATE('08/07/16 11:59 AM', '%m/%d/%y %l:%i %p');
+-------------------------------------------------------+
| STR_TO_DATE('08/07/16 11:59 AM', '%m/%d/%y %l:%i %p') |
+-------------------------------------------------------+
| 2016-08-07 11:59:00                                   |
+-------------------------------------------------------+
1 row in set (0.00 sec)

日期有效:

mysql> SELECT STR_TO_DATE('08/07/16', '%m/%d/%y');
+-------------------------------------+
| STR_TO_DATE('08/07/16', '%m/%d/%y') |
+-------------------------------------+
| 2016-08-07                          |
+-------------------------------------+
1 row in set (0.00 sec)

时间不是:

mysql> SELECT STR_TO_DATE('11:59 AM', '%l:%i %p');
+-------------------------------------+
| STR_TO_DATE('11:59 AM', '%l:%i %p') |
+-------------------------------------+
| NULL                                |
+-------------------------------------+
1 row in set, 1 warning (0.00 sec)

格式化时间有效:

mysql> SELECT DATE_FORMAT(NOW(), '%l:%i %p');
+--------------------------------+
| DATE_FORMAT(NOW(), '%l:%i %p') |
+--------------------------------+
| 1:58 PM                        |
+--------------------------------+
1 row in set (0.00 sec)

但是格式化时间然后使用相同的格式字符串将其转回来不会。

mysql> SELECT STR_TO_DATE(DATE_FORMAT(NOW(), '%l:%i %p'), '%l:%i %p');
+---------------------------------------------------------+
| STR_TO_DATE(DATE_FORMAT(NOW(), '%l:%i %p'), '%l:%i %p') |
+---------------------------------------------------------+
| NULL                                                    |
+---------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

作为一种解决方法,我最终做了:

mysql> SELECT TIME(STR_TO_DATE('08/07/16 11:59 AM', '%m/%d/%y %l:%i %p'));
+-------------------------------------------------------------+
| TIME(STR_TO_DATE('08/07/16 11:59 AM', '%m/%d/%y %l:%i %p')) |
+-------------------------------------------------------------+
| 11:59:00                                                    |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

数据以这种方式存储在一个巨大的csv文件中,因此编辑它会很痛苦。我只是想知道这是否正常,或者我是否忽视了一些简单的事情。

服务器信息是否重要:

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.13, for Linux (x86_64) using  EditLine wrapper

Connection id:          19
Current database:
Current user:           michael@localhost
SSL:                    Cipher in use is DHE-RSA-AES256-SHA
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.13-log MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db     characterset:    latin1
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 29 min 10 sec

1 个答案:

答案 0 :(得分:3)

请记住,有一种特殊的NO_ZERO_DATENO_ZERO_IN_DATE SQL模式可能会禁止零日期,因此如果您只解析一段时间,则STR_TO_DATE()会返回NULL

尝试运行以下命令,然后只运行STR_TO_DATE()

mysql> SET sql_mode = '';