- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set time data type to be only HH:MM in MySQL?
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)
Advertisements