sql >> Base de Datos >  >> RDS >> Mysql

Cómo convertir segundo a tiempo usando MYSQL

Simplemente puede escribir su propia función en MySQL así:

drop function if exists my_sec_to_time;
delimiter $$
create function my_sec_to_time(p_sec int)
returns varchar(10)
deterministic
begin
declare v_hour int;
declare v_minute int;

declare v_tmp_sec int;

set v_hour = floor(p_sec / 3600);
set v_tmp_sec = p_sec - (v_hour * 3600);
set v_minute = floor(v_tmp_sec / 60);
set v_tmp_sec = v_tmp_sec - (v_minute * 60);

return concat(v_hour, ':', v_minute, ':', v_tmp_sec);

end $$

delimiter ;

En acción:

mysql;[email protected](playground)> select my_sec_to_time(3020399);
+-------------------------+
| my_sec_to_time(3020399) |
+-------------------------+
| 838:59:59               |
+-------------------------+
1 row in set (0.00 sec)

mysql;[email protected](playground)> select my_sec_to_time(3020400);
+-------------------------+
| my_sec_to_time(3020400) |
+-------------------------+
| 839:0:0                 |
+-------------------------+
1 row in set (0.00 sec)

mysql;[email protected](playground)> select my_sec_to_time(4020400);
+-------------------------+
| my_sec_to_time(4020400) |
+-------------------------+
| 1116:46:40              |
+-------------------------+
1 row in set (0.00 sec)