- Trending Categories
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
Using Time datatype in MySQL without seconds?
You need to use DATE_FORMAT() for this. The syntax is as follows −
SELECT DATE_FORMAT(yourColumnName,'%k:%i') as anyAliasName FROM yourTableName;
You can use ‘%H:%i’ for the same result. To understand the above syntax, let us create a table.
The query to create a table is as follows −
mysql> create table TimeDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> LastLoginTime time -> ); Query OK, 0 rows affected (0.56 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into TimeDemo(LastLoginTime) values('09:30:35'); Query OK, 1 row affected (0.20 sec) mysql> insert into TimeDemo(LastLoginTime) values('10:45:30'); Query OK, 1 row affected (0.19 sec) mysql> insert into TimeDemo(LastLoginTime) values('13:33:58'); Query OK, 1 row affected (0.24 sec) mysql> insert into TimeDemo(LastLoginTime) values('15:45:31'); Query OK, 1 row affected (0.21 sec) mysql> insert into TimeDemo(LastLoginTime) values('16:40:36'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from TimeDemo;
The following is the output −
+----+---------------+ | Id | LastLoginTime | +----+---------------+ | 1 | 09:30:35 | | 2 | 10:45:30 | | 3 | 13:33:58 | | 4 | 15:45:31 | | 5 | 16:40:36 | +----+---------------+ 5 rows in set (0.00 sec)
Here is the query to get time without seconds. The query is as follows −
mysql> select DATE_FORMAT(LastLoginTime,'%k:%i') as `Time` from TimeDemo;
The following is the output −
+-------+ | Time | +-------+ | 9:30 | | 10:45 | | 13:33 | | 15:45 | | 16:40 | +-------+ 5 rows in set (0.00 sec)
You can change the format specifier like %H:%i to get the same result without seconds. The query is as follows −
mysql> select DATE_FORMAT(LastLoginTime,' %H:%i') as `Time` from TimeDemo;
The following is the output −
+--------+ | Time | +--------+ | 09:30 | | 10:45 | | 13:33 | | 15:45 | | 16:40 | +--------+ 5 rows in set (0.00 sec)
Advertisements