Mega Code Archive

 
Categories / MySQL / Date Time
 

Treating Dates or Times as Numbers

mysql> mysql> mysql> CREATE TABLE time_val     -> (     ->  t1      TIME,     ->  t2      TIME     -> ); Query OK, 0 rows affected (0.01 sec) mysql> mysql> INSERT INTO time_val (t1,t2) VALUES('15:00:00','15:00:00'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO time_val (t1,t2) VALUES('05:01:30','02:30:20'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO time_val (t1,t2) VALUES('12:30:20','17:30:45'); Query OK, 1 row affected (0.00 sec) mysql> mysql> SELECT * FROM time_val; +----------+----------+ | t1       | t2       | +----------+----------+ | 15:00:00 | 15:00:00 | | 05:01:30 | 02:30:20 | | 12:30:20 | 17:30:45 | +----------+----------+ 3 rows in set (0.00 sec) mysql> mysql> mysql> SELECT t1,     -> t1+0 AS 't1 as number',     -> FLOOR(t1) AS 't1 as number',     -> FLOOR(t1/10000) AS 'hour part'     -> FROM time_val; +----------+--------------+--------------+-----------+ | t1       | t1 as number | t1 as number | hour part | +----------+--------------+--------------+-----------+ | 15:00:00 |       150000 |       150000 |        15 | | 05:01:30 |        50130 |        50130 |         5 | | 12:30:20 |       123020 |       123020 |        12 | +----------+--------------+--------------+-----------+ 3 rows in set (0.00 sec) mysql> mysql> drop table time_val; Query OK, 0 rows affected (0.00 sec)