Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Time Difference w.r.t Dates

Time Difference w.r.t Dates

Scheduled Pinned Locked Moved Database
csharpmcpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliAmjad
    wrote on last edited by
    #1

    Actually i just want to output the difference of two Timespans with respect to its dates e.g. 07/03/2009 05:00:00 07/04/2009 10:00:00 and the difference i get is 05:00:00 which is not correct i am using the following code:DECLARE @EnterTime datetime, @DischargeTime datetime Select @EnterTime = convert(varchar,EnterTime,8) from visit Select @DischargeTime = convert(varchar,NextTime,8) from visit Select convert(varchar,@DischargeTime - @EnterTime,8)
    the above code is working fine if the duration is between 24 hours. Can you please suggest me a solution or i have to apply the logic on programmatic side i.e. .NET?

    AliAmjad(MCP) First make it Run THEN make it Run Fast!

    B 1 Reply Last reply
    0
    • A AliAmjad

      Actually i just want to output the difference of two Timespans with respect to its dates e.g. 07/03/2009 05:00:00 07/04/2009 10:00:00 and the difference i get is 05:00:00 which is not correct i am using the following code:DECLARE @EnterTime datetime, @DischargeTime datetime Select @EnterTime = convert(varchar,EnterTime,8) from visit Select @DischargeTime = convert(varchar,NextTime,8) from visit Select convert(varchar,@DischargeTime - @EnterTime,8)
      the above code is working fine if the duration is between 24 hours. Can you please suggest me a solution or i have to apply the logic on programmatic side i.e. .NET?

      AliAmjad(MCP) First make it Run THEN make it Run Fast!

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Do you want to get days or hours or minutes between two dates?


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      A 1 Reply Last reply
      0
      • B Blue_Boy

        Do you want to get days or hours or minutes between two dates?


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        A Offline
        A Offline
        AliAmjad
        wrote on last edited by
        #3

        I want to get time in HH:mm:ss format e.g. 13:15:02

        AliAmjad(MCP) First make it Run THEN make it Run Fast!

        B 1 Reply Last reply
        0
        • A AliAmjad

          I want to get time in HH:mm:ss format e.g. 13:15:02

          AliAmjad(MCP) First make it Run THEN make it Run Fast!

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          This will do your trick.

          DECLARE @d1 DATETIME, @d2 DATETIME, @sd INT

          SET @d1 = '20090706 00:00:00'
          SET @d2 = '20090707 01:30:01'

          SET @sd = DATEDIFF(SECOND, @d1, @d2)

          SELECT
          CASE WHEN @sd/3600<10 THEN '0' ELSE '' END

          • RTRIM(@sd/3600)
          • ':' + RIGHT('0'+RTRIM((@sd % 3600) / 60),2)
          • ':' + RIGHT('0'+RTRIM((@sd % 3600) % 60),2) AS Hours

          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          A 1 Reply Last reply
          0
          • B Blue_Boy

            This will do your trick.

            DECLARE @d1 DATETIME, @d2 DATETIME, @sd INT

            SET @d1 = '20090706 00:00:00'
            SET @d2 = '20090707 01:30:01'

            SET @sd = DATEDIFF(SECOND, @d1, @d2)

            SELECT
            CASE WHEN @sd/3600<10 THEN '0' ELSE '' END

            • RTRIM(@sd/3600)
            • ':' + RIGHT('0'+RTRIM((@sd % 3600) / 60),2)
            • ':' + RIGHT('0'+RTRIM((@sd % 3600) % 60),2) AS Hours

            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            A Offline
            A Offline
            AliAmjad
            wrote on last edited by
            #5

            Thanks Bro... :)

            AliAmjad(MCP) First make it Run THEN make it Run Fast!

            B 1 Reply Last reply
            0
            • A AliAmjad

              Thanks Bro... :)

              AliAmjad(MCP) First make it Run THEN make it Run Fast!

              B Offline
              B Offline
              Blue_Boy
              wrote on last edited by
              #6

              You are welcome.


              I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups