Bug #43774 CAST(CAST(value) as TIME as DATETIME ) returns the wrong value.
Submitted: 20 Mar 2009 23:26 Modified: 10 Sep 2013 13:26
Reporter: Justin Swanhart Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: DML Severity:S1 (Critical)
Version:5.1.30, 4.1, 5.0, 5.1 OS:Any
Assigned to: CPU Architecture:Any
Tags: cast, datetime, time, wrong result

[20 Mar 2009 23:26] Justin Swanhart
Description:
Casting of a TIME value to DATETIME treats the time as a string, resulting in a wrong DATETIME without the TIME portion set.

How to repeat:
mysql> select cast(cast('21:00:00' as time) as datetime);
+--------------------------------------------+
| cast(cast('21:00:00' as time) as datetime) |
+--------------------------------------------+
| 2021-00-00 00:00:00                        |
+--------------------------------------------+
1 row in set (0.00 sec)

Suggested fix:
CAST to DATETIME from a TIME should yield:
0000-00-00 HH:MI:SS, where 'HH:MI:SS' in the input TIME value in the CAST.
[20 Mar 2009 23:57] Justin Swanhart
Updating the summary to reflect that double cast is being done.

I can understand why CAST('HH:MM:SS' as datetime) returns a bad date, since MySQL will (in my opinion wrongly) try to extract MM-DD-YY format from the input string.

However, when an explicit CAST to TIME is given, even for column references, MySQL returns the wrong result.

CREATE TABLE t1 (f1 time, f2 time);
insert into t1 values ('09:00:00','12:00:00');

mysql> select f1, f2, cast(f1 as datetime), cast(cast(f1 as TIME) as datetime) from t1;
+----------+----------+----------------------+------------------------------------+
| f1       | f2       | cast(f1 as datetime) | cast(cast(f1 as TIME) as datetime) |
+----------+----------+----------------------+------------------------------------+
| 09:00:00 | 12:00:00 | 0000-00-00 09:00:00  | 2009-00-00 00:00:00                |
+----------+----------+----------------------+------------------------------------+
1 row in set (0.00 sec)
[23 Mar 2009 7:22] Sveta Smirnova
Thank you for the report.

Verified as described.
[6 Sep 2013 18:02] Valeriy Kravchuk
To whom it may concern, 

This is what we have in MySQL 5.6:

C:\Program Files\MySQL\MySQL Server 5.5\bin>mysql -uroot -proot -P3314 test
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select cast(cast('21:00:00' as time) as datetime);
+--------------------------------------------+
| cast(cast('21:00:00' as time) as datetime) |
+--------------------------------------------+
| 2013-09-06 21:00:00                        |
+--------------------------------------------+
1 row in set (0.03 sec)

mysql> select cast(cast('19:00:00' as time) as datetime);
+--------------------------------------------+
| cast(cast('19:00:00' as time) as datetime) |
+--------------------------------------------+
| 2013-09-06 19:00:00                        |
+--------------------------------------------+
1 row in set (0.00 sec)

Same bug is present in 5.5.33 though.
[7 Sep 2013 8:39] MySQL Verification Team
mysql>  select cast(cast('21:00:00' as time) as datetime)a,version();
+---------------------+----------------+
| a                   | version()      |
+---------------------+----------------+
| 2021-00-00 00:00:00 | 5.6.3-m6-debug |
+---------------------+----------------+
1 row in set (0.00 sec)

.....

mysql>  select cast(cast('21:00:00' as time) as datetime)a,version();
+---------------------+----------------+
| a                   | version()      |
+---------------------+----------------+
| 2013-09-07 21:00:00 | 5.6.4-m7-debug |
+---------------------+----------------+
1 row in set (0.00 sec)

....
http://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-4.html

I do not know the exact revision that fixed it, I guess somebody should run bzrfind.
[7 Sep 2013 12:26] jiang xiaobing
http://bugs.mysql.com/bug.php?id=69352

I think this bug is the same reason like my report one.
[7 Sep 2013 14:22] Roy Lyseng
5.6.4 added Fractional Seconds Handling, and I think that worklog also added standard-compliant
cast of TIME to DATETIME.
[10 Sep 2013 13:26] Benny Wang
Fixed revno:3402.30.1