Convert MySQL time from HH:MM:SS to HH:MM

MySQLMySQLi Database

To convert, use MySQL TIME_FORMAT(). Let us first create a −

mysql> create table DemoTable1419
   -> (
   -> ArrivalTime time
   -> );
Query OK, 0 rows affected (0.78 sec)

Insert some records in the table using insert command. Here, we have inserted time records −

mysql> insert into DemoTable1419 values('12:30:45');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1419 values('11:00:55');
Query OK, 1 row affected (0.45 sec)
mysql> insert into DemoTable1419 values('09:59:34');
Query OK, 1 row affected (0.16 sec)

Display all records from the table using select −

mysql> select * from DemoTable1419;

This will produce the following output −

+-------------+
| ArrivalTime |
+-------------+
| 12:30:45    |
| 11:00:55    |
| 09:59:34    |
+-------------+
3 rows in set (0.00 sec)

Following is the query to convert MySQL time from HH:MM:SS to HH:MM −

mysql> select ArrivalTime,time_format(ArrivalTime,'%H:%i') from DemoTable1419;

This will produce the following output −

+-------------+----------------------------------+
| ArrivalTime | time_format(ArrivalTime,'%H:%i') |
+-------------+----------------------------------+
| 12:30:45    | 12:30                            |
| 11:00:55    | 11:00                            |
| 09:59:34    | 09:59                            |
+-------------+----------------------------------+
3 rows in set (0.05 sec)
raja
Published on 12-Nov-2019 05:28:20
Advertisements