- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Remove seconds from time field in MySQL?
You need to use TIME_FORMAT() to remove seconds from time field. The syntax is as follows:
SELECT TIME_FORMAT(yourColumnName1, "%H:%i") AS anyVariableName, TIME_FORMAT(yourColumnName2, "%H:%i") AS anyVariableName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table removeSecondsFromTime -> ( -> Id int NOT NULL AUTO_INCREMENT, -> StartTime time, -> EndTime time, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.71 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('10:20:45','11:21:40'); Query OK, 1 row affected (0.43 sec) mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('12:30:55','13:20:21'); Query OK, 1 row affected (0.31 sec) mysql> insert into removeSecondsFromTime(StartTime,EndTime) values('14:40:57','15:55:02'); Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement. The query is as follows:
mysql> select *from removeSecondsFromTime;
The following is the output;
+----+-----------+----------+ | Id | StartTime | EndTime | +----+-----------+----------+ | 1 | 10:20:45 | 11:21:40 | | 2 | 12:30:55 | 13:20:21 | | 3 | 14:40:57 | 15:55:02 | +----+-----------+----------+ 3 rows in set (0.00 sec)
Here is the query to remove seconds from time field:
mysql> select TIME_FORMAT(StartTime, "%H:%i") as StartingTime, -> TIME_FORMAT(EndTime, "%H:%i") as EndingTime -> from removeSecondsFromTime;
The following is the output;
+--------------+------------+ | StartingTime | EndingTime | +--------------+------------+ | 10:20 | 11:21 | | 12:30 | 13:20 | | 14:40 | 15:55 | +--------------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- Remove seconds from MySQL Datetime?
- Using Time datatype in MySQL without seconds?
- Remove 20% from stored price in MySQL field, using SQL query?
- How to remove special characters from a database field in MySQL?
- How to remove -XXX from Zip Code field using MySQL REGEXP?
- How to calculate time based on seconds in MySQL?
- How to remove leading and trailing whitespace from a MySQL field value?
- MySQL query to extract time in a format without seconds
- How to sum time in MySQL by converting into seconds?
- Subtract seconds from current time using Calendar.add() method in Java
- Remove Seconds/ Milliseconds from Date and convert to ISO String?
- How to remove leading and trailing whitespace in a MySQL field?
- How to get (format) date and time from mill seconds in Java?
- Add DATE and TIME fields to get DATETIME field in MySQL?
- Comparing dates in MySQL ignoring time portion of a DateTime field?

Advertisements