How to set time data type to be only HH:MM in MySQL?

MySQLMySQLi Database

You can use DATE_FORMAT() to set time data type to be only HH:MM. 

Following is the syntax −

select DATE_FORMAT(yourColumnName, "%H:%i") AS anyAliasName from yourTableName;

Let us first create a table −

mysql> create table DemoTable
(
   Arrivaltime time
);
Query OK, 0 rows affected (0.61 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values('08:20');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('05:40');
Query OK, 1 row affected (0.12 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| Arrivaltime |
+-------------+
| 08:20:00    |
| 05:40:00    |
+-------------+
2 rows in set (0.00 sec)

Here is the query to set only HH:MM in MySQL database −

mysql> select DATE_FORMAT(Arrivaltime, "%H:%i") AS ONLY_HOUR_MINUTE from DemoTable;

This will produce the following output −

+------------------+
| ONLY_HOUR_MINUTE |
+------------------+
| 08:20            |
| 05:40            |
+------------------+
2 rows in set (0.03 sec)
raja
Updated on 30-Jul-2019 22:30:25

Advertisements