为此,您可以使用TIME_FORMAT()。让我们首先创建一个表-mysql> create table DemoTable1591
-> (
-> ArrivalTime varchar(20)
-> );
使用插入命令在表中插入一些记录-mysql> insert into DemoTable1591 values('1620');
mysql> insert into DemoTable1591 values('2345');
mysql> insert into DemoTable1591 values('2210');
使用select语句显示表中的所有记录-mysql> select * from DemoTable1591;
这将产生以下输出-+-------------+
| ArrivalTime |
+-------------+
| 1620 |
| 2345 |
| 2210 |
+-------------+
3 rows in set (0.00 sec)
这是在MySQL中将varchar“时间”转换为实时的查询-mysql> select TIME_FORMAT(CONCAT(SUBSTRING(ArrivalTime, 1,2), ':', SUBSTRING(ArrivalTime, 3,4)), '%h:%i') from DemoTable1591;
这将产生以下输出-+---------------------------------------------------------------------------------------------+
| TIME_FORMAT(CONCAT(SUBSTRING(ArrivalTime, 1,2), ':', SUBSTRING(ArrivalTime, 3,4)), '%h:%i') |
+---------------------------------------------------------------------------------------------+
| 04:20 |
| 11:45 |
| 10:10 |
+---------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)