Round seconds to nearest half minute in MySQL?


To round seconds to nearest half minute, use CEILING(). Let us first create a table −

mysql> create table DemoTable (secondValue int);
Query OK, 0 rows affected (0.64 sec)

Example

Insert some records in the table using insert command −

mysql> insert into DemoTable values(27);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(56);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(118);
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-------------+
| secondValue |
+-------------+
| 27          |
| 56          |
| 118         |
+-------------+
3 rows in set (0.00 sec)

Following is the query to round seconds −

mysql> select secondValue, CEILING(secondValue / 30) / 2 AS ApproxMinutes from DemoTable;

Output

+-------------+---------------+
| secondValue | ApproxMinutes |
+-------------+---------------+
| 27          | 0.5000        |
| 56          | 1.0000        |
| 118         | 2.0000        |
+-------------+---------------+
3 rows in set (0.00 sec)

Updated on: 02-Jul-2020

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements