STR_TO_DATE 无法使用生成的 TIME 格式,需要手动输入值,只有生成的 DATATIME 和 DATE 格式可以

STR_TO_DATE can't work with generated TIME formats and values needs to be entered manually, only generated DATATIME and DATE formats work

这里我有 date1 和 time1 作为 -

SELECT @date1;
+----------+
| @date1   |
+----------+
| %D %b %Y |
+----------+
SELECT @time1;
+--------------------+
| @time1             |
+--------------------+
| %Ih : %im : %ss %p |
+--------------------+

现在,我有以下有效查询 -

SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), @date1) AS output2, STR_TO_DATE(DATE_FORMAT(NOW(6), @date1), @date1) AS output3;
+----------------------------+---------------+----------------------------+
| output1                    | output2       | output3                    |
+----------------------------+---------------+----------------------------+
| 2021-12-27 02:08:18.282722 | 27th Dec 2021 | 2021-12-27 00:00:00.000000 |
+----------------------------+---------------+----------------------------+
SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), CONCAT_WS(SPACE(2), @date1, @time1)) AS output2, STR_TO_DATE(DATE_FORMAT(NOW(6), CONCAT_WS(SPACE(2), @date1, @time1)), CONCAT_WS(SPACE(2), @date1, @time1)) AS output3;
+----------------------------+-----------------------------------+----------------------------+
| output1                    | output2                           | output3                    |
+----------------------------+-----------------------------------+----------------------------+
| 2021-12-27 02:12:48.713278 | 27th Dec 2021  02h : 12m : 48s AM | 2021-12-27 02:12:48.000000 |
+----------------------------+-----------------------------------+----------------------------+

现在我尝试以下 -

SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), @time1) AS output2, STR_TO_DATE(DATE_FORMAT(NOW(6), @time1), @time1) AS output3;
+----------------------------+--------------------+---------+
| output1                    | output2            | output3 |
+----------------------------+--------------------+---------+
| 2021-12-27 02:20:12.335700 | 02h : 20m : 12s AM | NULL    |
+----------------------------+--------------------+---------+
SELECT CURTIME(6) AS output1, DATE_FORMAT(CURTIME(6), @time1) AS output2, STR_TO_DATE(DATE_FORMAT(CURTIME(6), @time1), @time1) AS output3;
+-----------------+--------------------+---------+
| output1         | output2            | output3 |
+-----------------+--------------------+---------+
| 02:16:53.952314 | 02h : 16m : 53s AM | NULL    |
+-----------------+--------------------+---------+
SELECT CURTIME(6) AS output1, TIME_FORMAT(CURTIME(6), @time1) AS output2, STR_TO_DATE(TIME_FORMAT(CURTIME(6), @time1), @time1) AS output3;
+-----------------+--------------------+---------+
| output1         | output2            | output3 |
+-----------------+--------------------+---------+
| 02:18:33.676080 | 02h : 18m : 33s AM | NULL    |
+-----------------+--------------------+---------+

在上述所有情况下,我得到 output3 作为 NULL

似乎只有我手动输入值才能生成所需的结果 -

SELECT STR_TO_DATE("02h : 26m : 26s AM", "%Ih : %im : %ss %p") AS output;
+----------+
| output   |
+----------+
| 02:26:26 |
+----------+

即使下面的查询似乎也不起作用 -

SELECT STR_TO_DATE("02h : 26m : 26s AM", @time1) AS output;
+--------+
| output |
+--------+
| NULL   |
+--------+

现在,这很奇怪。如果 date 格式存在或不存在 time 格式,则 STR_TO_DATE 可以很好地处理生成的值。但是,如果只有 time 格式而没有任何 date 格式,则 STR_TO_DATE 不再适用于生成的值。在这种情况下 STR_TO_DATE 仅适用于手动输入的值。

这显然是一个巨大的不便,因为很少有人只手动输入值,并且在实际应用中通常会使用生成的值。

现在 MySQL 中没有 STR_TO_TIME

那么有什么解决方法吗?还是唯一的前进方式是提出功能请求并等待将来的新改进版本?


更新:

有趣的是,下面的工作非常完美 -

SELECT NOW(6) AS output1, DATE_FORMAT(NOW(6), GET_FORMAT(TIME, "USA")) AS output2, STR_TO_DATE(DATE_FORMAT(NOW(6), GET_FORMAT(TIME, "USA")), GET_FORMAT(TIME, "USA")) AS output3;
+----------------------------+-------------+----------+
| output1                    | output2     | output3  |
+----------------------------+-------------+----------+
| 2021-12-27 03:26:04.787166 | 03:26:04 AM | 03:26:04 |
+----------------------------+-------------+----------+

我以前遇到过类似的事情,似乎在 MySQL v5.7 中,你不能 STR_TO_DATE 单独 time 值,你需要date 值也是如此。这有效:

SET @time1 = '%Y-%m-%d %Ih : %im : %ss %p';

SELECT TIME(STR_TO_DATE(DATE_FORMAT(NOW(6), @time1), '%Y-%m-%d %Ih : %im : %ss %p'))

但由于 STR_TO_DATE() 的最终输出包含日期,因此需要 TIME() 函数来仅提取时间值。

Demo fiddle.

可能因为函数被“命名”为STR_TO_DATE,仅转换时间值是没有意义的。这是可以理解的,但也很奇怪,因为在 MySQL 8.0 上,it doesn't behave like that.