MySQL select formatted date from millisecond field

后端 未结 3 673
野的像风
野的像风 2021-02-02 09:10

I have a column in a MySQL database that contains a date as milliseconds (epoch). I want to build an SQL query that formats the date as something human readable (day, month, yea

3条回答
  •  面向向阳花
    2021-02-02 09:31

    CREATE DEFINER=`root`@`localhost` FUNCTION `ConvertTimeDelta`(duration int) RETURNS 
    varchar(20) CHARSET utf8mb4
        DETERMINISTIC
    BEGIN
     DECLARE time_delta VARCHAR(20);
     DECLARE date_format VARCHAR(20);
     SET date_format = IF (duration > 3599999, '%H:%i:%s', '%i:%s');
     SET time_delta = TIME_FORMAT(SEC_TO_TIME(duration/1000), date_format);
     RETURN time_delta;
    END
    

    For example select ConvertTimeDelta(4800240) result = 01:20:00 select ConvertTimeDelta(35076) result = 00:35

提交回复
热议问题