Convert seconds into HH:MM:SS format in MySQL












0















I have a query where I'm pulling data from the Zultys VoIP Database, about employees and their call in/call out numbers, and the time they spent over the phone: Time in/Time out in seconds, and their phone extensions as well.



The query is below:



SELECT 
IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION) AS Ext,
SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
IF(SESSIONIDTRANSFEREDTO IS NULL,
Duration,
HoldTimeSecs),
0)) AS TimeIn,
SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
IF(SESSIONIDTRANSFEREDTO IS NULL,
Duration,
HoldTimeSecs),
0)) AS TimeOut,
SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
1,
0)) AS CallIn,
SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
1,
0)) AS CallOut
FROM
((session
INNER JOIN callsummary ON session.NOTABLECALLID = callsummary.NOTABLECALLID
AND DATEDIFF(CURDATE(), STARTTIME) = 1)
LEFT JOIN mxuser ON session.EXTENSIONID1 = mxuser.EXTENSIONID)
LEFT JOIN
mxuser AS mxuser_1 ON session.EXTENSIONID2 = mxuser_1.EXTENSIONID
WHERE
SESSIONTYPE = 1 AND ANSWERED = 1
AND (DIALPLANNAME NOT LIKE 'Local %'
AND TRUNKGROUPNAME1 LIKE 'dev.%'
AND TRUNKGROUPNAME2 = 'itsp.Nextiva')
OR (TRUNKGROUPNAME1 = 'itsp.Nextiva'
AND TRUNKGROUPNAME2 LIKE 'dev.%'
AND CALLINGPARTYNO NOT LIKE '404%'
AND CALLINGPARTYNO NOT LIKE '678%'
AND CALLINGPARTYNO NOT LIKE '770%')
GROUP BY IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION)
HAVING Ext IS NOT NULL;


When I run this query above I get this result:



enter image description here



What I'm trying to achieve is to add TimeIn and TimeOut as total time and to convert seconds to this format HH:MM:SS.



Any thoughts?










share|improve this question



























    0















    I have a query where I'm pulling data from the Zultys VoIP Database, about employees and their call in/call out numbers, and the time they spent over the phone: Time in/Time out in seconds, and their phone extensions as well.



    The query is below:



    SELECT 
    IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION) AS Ext,
    SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
    IF(SESSIONIDTRANSFEREDTO IS NULL,
    Duration,
    HoldTimeSecs),
    0)) AS TimeIn,
    SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
    IF(SESSIONIDTRANSFEREDTO IS NULL,
    Duration,
    HoldTimeSecs),
    0)) AS TimeOut,
    SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
    1,
    0)) AS CallIn,
    SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
    1,
    0)) AS CallOut
    FROM
    ((session
    INNER JOIN callsummary ON session.NOTABLECALLID = callsummary.NOTABLECALLID
    AND DATEDIFF(CURDATE(), STARTTIME) = 1)
    LEFT JOIN mxuser ON session.EXTENSIONID1 = mxuser.EXTENSIONID)
    LEFT JOIN
    mxuser AS mxuser_1 ON session.EXTENSIONID2 = mxuser_1.EXTENSIONID
    WHERE
    SESSIONTYPE = 1 AND ANSWERED = 1
    AND (DIALPLANNAME NOT LIKE 'Local %'
    AND TRUNKGROUPNAME1 LIKE 'dev.%'
    AND TRUNKGROUPNAME2 = 'itsp.Nextiva')
    OR (TRUNKGROUPNAME1 = 'itsp.Nextiva'
    AND TRUNKGROUPNAME2 LIKE 'dev.%'
    AND CALLINGPARTYNO NOT LIKE '404%'
    AND CALLINGPARTYNO NOT LIKE '678%'
    AND CALLINGPARTYNO NOT LIKE '770%')
    GROUP BY IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION)
    HAVING Ext IS NOT NULL;


    When I run this query above I get this result:



    enter image description here



    What I'm trying to achieve is to add TimeIn and TimeOut as total time and to convert seconds to this format HH:MM:SS.



    Any thoughts?










    share|improve this question

























      0












      0








      0








      I have a query where I'm pulling data from the Zultys VoIP Database, about employees and their call in/call out numbers, and the time they spent over the phone: Time in/Time out in seconds, and their phone extensions as well.



      The query is below:



      SELECT 
      IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION) AS Ext,
      SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
      IF(SESSIONIDTRANSFEREDTO IS NULL,
      Duration,
      HoldTimeSecs),
      0)) AS TimeIn,
      SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
      IF(SESSIONIDTRANSFEREDTO IS NULL,
      Duration,
      HoldTimeSecs),
      0)) AS TimeOut,
      SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
      1,
      0)) AS CallIn,
      SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
      1,
      0)) AS CallOut
      FROM
      ((session
      INNER JOIN callsummary ON session.NOTABLECALLID = callsummary.NOTABLECALLID
      AND DATEDIFF(CURDATE(), STARTTIME) = 1)
      LEFT JOIN mxuser ON session.EXTENSIONID1 = mxuser.EXTENSIONID)
      LEFT JOIN
      mxuser AS mxuser_1 ON session.EXTENSIONID2 = mxuser_1.EXTENSIONID
      WHERE
      SESSIONTYPE = 1 AND ANSWERED = 1
      AND (DIALPLANNAME NOT LIKE 'Local %'
      AND TRUNKGROUPNAME1 LIKE 'dev.%'
      AND TRUNKGROUPNAME2 = 'itsp.Nextiva')
      OR (TRUNKGROUPNAME1 = 'itsp.Nextiva'
      AND TRUNKGROUPNAME2 LIKE 'dev.%'
      AND CALLINGPARTYNO NOT LIKE '404%'
      AND CALLINGPARTYNO NOT LIKE '678%'
      AND CALLINGPARTYNO NOT LIKE '770%')
      GROUP BY IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION)
      HAVING Ext IS NOT NULL;


      When I run this query above I get this result:



      enter image description here



      What I'm trying to achieve is to add TimeIn and TimeOut as total time and to convert seconds to this format HH:MM:SS.



      Any thoughts?










      share|improve this question














      I have a query where I'm pulling data from the Zultys VoIP Database, about employees and their call in/call out numbers, and the time they spent over the phone: Time in/Time out in seconds, and their phone extensions as well.



      The query is below:



      SELECT 
      IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION) AS Ext,
      SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
      IF(SESSIONIDTRANSFEREDTO IS NULL,
      Duration,
      HoldTimeSecs),
      0)) AS TimeIn,
      SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
      IF(SESSIONIDTRANSFEREDTO IS NULL,
      Duration,
      HoldTimeSecs),
      0)) AS TimeOut,
      SUM(IF(TRUNKGROUPNAME2 != 'itsp.Nextiva',
      1,
      0)) AS CallIn,
      SUM(IF(TRUNKGROUPNAME2 = 'itsp.Nextiva',
      1,
      0)) AS CallOut
      FROM
      ((session
      INNER JOIN callsummary ON session.NOTABLECALLID = callsummary.NOTABLECALLID
      AND DATEDIFF(CURDATE(), STARTTIME) = 1)
      LEFT JOIN mxuser ON session.EXTENSIONID1 = mxuser.EXTENSIONID)
      LEFT JOIN
      mxuser AS mxuser_1 ON session.EXTENSIONID2 = mxuser_1.EXTENSIONID
      WHERE
      SESSIONTYPE = 1 AND ANSWERED = 1
      AND (DIALPLANNAME NOT LIKE 'Local %'
      AND TRUNKGROUPNAME1 LIKE 'dev.%'
      AND TRUNKGROUPNAME2 = 'itsp.Nextiva')
      OR (TRUNKGROUPNAME1 = 'itsp.Nextiva'
      AND TRUNKGROUPNAME2 LIKE 'dev.%'
      AND CALLINGPARTYNO NOT LIKE '404%'
      AND CALLINGPARTYNO NOT LIKE '678%'
      AND CALLINGPARTYNO NOT LIKE '770%')
      GROUP BY IFNULL(mxuser.EXTENSION, mxuser_1.EXTENSION)
      HAVING Ext IS NOT NULL;


      When I run this query above I get this result:



      enter image description here



      What I'm trying to achieve is to add TimeIn and TimeOut as total time and to convert seconds to this format HH:MM:SS.



      Any thoughts?







      mysql






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 12 '17 at 19:47









      Blackcoat77Blackcoat77

      1155




      1155






















          3 Answers
          3






          active

          oldest

          votes


















          4














          https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html



          see




          • GET_FORMAT()

          • SEC_TO_TIME()

          • TIME_FORMAT()


          and possibly others.



          Not knowing the datatype of your values, I can't be more specific.






          share|improve this answer































            2














            I think SEC_TO_TIME function is what you need here if I correctly understood your request: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time



            You may read more about date and time functions reading this documentation: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html






            share|improve this answer































              0














              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






              share|improve this answer








              New contributor




              Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "182"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f179688%2fconvert-seconds-into-hhmmss-format-in-mysql%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4














                https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html



                see




                • GET_FORMAT()

                • SEC_TO_TIME()

                • TIME_FORMAT()


                and possibly others.



                Not knowing the datatype of your values, I can't be more specific.






                share|improve this answer




























                  4














                  https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html



                  see




                  • GET_FORMAT()

                  • SEC_TO_TIME()

                  • TIME_FORMAT()


                  and possibly others.



                  Not knowing the datatype of your values, I can't be more specific.






                  share|improve this answer


























                    4












                    4








                    4







                    https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html



                    see




                    • GET_FORMAT()

                    • SEC_TO_TIME()

                    • TIME_FORMAT()


                    and possibly others.



                    Not knowing the datatype of your values, I can't be more specific.






                    share|improve this answer













                    https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html



                    see




                    • GET_FORMAT()

                    • SEC_TO_TIME()

                    • TIME_FORMAT()


                    and possibly others.



                    Not knowing the datatype of your values, I can't be more specific.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 12 '17 at 19:53









                    Rick JamesRick James

                    43.7k22259




                    43.7k22259

























                        2














                        I think SEC_TO_TIME function is what you need here if I correctly understood your request: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time



                        You may read more about date and time functions reading this documentation: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html






                        share|improve this answer




























                          2














                          I think SEC_TO_TIME function is what you need here if I correctly understood your request: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time



                          You may read more about date and time functions reading this documentation: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html






                          share|improve this answer


























                            2












                            2








                            2







                            I think SEC_TO_TIME function is what you need here if I correctly understood your request: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time



                            You may read more about date and time functions reading this documentation: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html






                            share|improve this answer













                            I think SEC_TO_TIME function is what you need here if I correctly understood your request: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_sec-to-time



                            You may read more about date and time functions reading this documentation: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 12 '17 at 19:56









                            user54user54

                            1304




                            1304























                                0














                                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






                                share|improve this answer








                                New contributor




                                Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                Check out our Code of Conduct.

























                                  0














                                  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






                                  share|improve this answer








                                  New contributor




                                  Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                  Check out our Code of Conduct.























                                    0












                                    0








                                    0







                                    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






                                    share|improve this answer








                                    New contributor




                                    Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.










                                    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







                                    share|improve this answer








                                    New contributor




                                    Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    share|improve this answer



                                    share|improve this answer






                                    New contributor




                                    Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.









                                    answered 13 mins ago









                                    HardikkumaHardikkuma

                                    1




                                    1




                                    New contributor




                                    Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.





                                    New contributor





                                    Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






                                    Hardikkuma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                    Check out our Code of Conduct.






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Database Administrators Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f179688%2fconvert-seconds-into-hhmmss-format-in-mysql%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        ف. موراي أبراهام

                                        صرب

                                        كأس إنترتوتو