Converting DateDiff to HH:MM:SS

  • Evening All,

    We're having some issues with some data and i will do my best to explain,

    The question relates to vehicle Tracking

    So, we have Vehicle ID, Journey Start TIme, Journey End Time, Journey Duration.

    Vehicle ID is Int

    Journey Start Time and End Time is DateTime (YYYY:MM:DD hh:mm:ss)

    Journey Duration is (hh:mm:ss)

    So, to calculate Journey duration we have applied the following

    Convert(varchar, Start_Time - End_Time, 108) which gives us our journey duration for a single journey.

    Now the issue we have is we would like to know the total journey duration for a vehicle and we are having some issues in obtaining the sum of the journey duration,

    What we are trying to do:

    Select Distinct(ID), Sum(Convert(varchar, Start_Time - End_Time, 108)),

    but we are always ending up with errors,

    so we tried a datediff between start time and end time and mangage to get the journey time in seconds,

    Now what we'd like to do is sum the number of seconds and then convert this to hh:mm:ss but it doesnt like us!!!

    I hope you can help this has been an extremely unproductive and frustrating day

    Many Thanks

    Waqqas.Zia

  • What is the code you tried that didn't work?

  • Here is an example using backupset. If I understood what you were asking for this should be it. It outputs the data as datatype TIME. If you want datetime then replace CAST('00:00:00' AS TIME)) with '1/1/1900'.

    SELECT database_name, DATEADD(ss,SUM(datediff(ss,backup_start_date, backup_finish_date)),CAST('00:00:00' AS TIME))

    FROM backupset

    GROUP BY database_name

    Kenneth FisherI was once offered a wizards hat but it got in the way of my dunce cap.--------------------------------------------------------------------------------For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]Link to my Blog Post --> www.SQLStudies.com[/url]

  • Great post just what I was looking for. You are in fact the man. Thanks

  • Thanks For the post. Saved my day

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply