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 Diffrence in SQL

Time Diffrence in SQL

Scheduled Pinned Locked Moved Database
questiondatabase
6 Posts 5 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.
  • S Offline
    S Offline
    sandhya14
    wrote on last edited by
    #1

    Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...

    I J N 3 Replies Last reply
    0
    • S sandhya14

      Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...

      I Offline
      I Offline
      i j russell
      wrote on last edited by
      #2

      Start by looking at the DATEDIFF function.

      1 Reply Last reply
      0
      • S sandhya14

        Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Here you go.. pretty darn simples.

        declare @dt1 as datetime
        set @dt1 = '2008-12-12 13:46:26'
        declare @dt2 as datetime
        set @dt2 = '2008-12-12 23:48:06'

        declare @dt3 datetime
        set @dt3 = @dt2-@dt1
        select CONVERT(VARCHAR(10),@dt3,108)

        108 is the format for HH:mi:ss

        1 Reply Last reply
        0
        • S sandhya14

          Hi all, I have a table like :- Start Time End Time ---------- --------- 12/12/2008 1:46:26 PM 12/12/2008 11:48:06 PM how can i get the diffrence between these two dates? (it should be in HH:MM:SS format.....) Thanks in Advance...

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          Mr. J4amieC solution is perfect which will work everywhere. However, if you are using Sql Server 2008, you can use the new TIME datatype something like this declare @tbl table(startdate datetime,enddate datetime) insert into @tbl select '2008-12-12 13:46:26','2008-12-12 23:48:06'

          select CAST((select enddate-startdate from @tbl) as time) TimeDiff

          :)

          Niladri Biswas

          modified on Wednesday, October 28, 2009 5:56 AM

          N 1 Reply Last reply
          0
          • N Niladri_Biswas

            Mr. J4amieC solution is perfect which will work everywhere. However, if you are using Sql Server 2008, you can use the new TIME datatype something like this declare @tbl table(startdate datetime,enddate datetime) insert into @tbl select '2008-12-12 13:46:26','2008-12-12 23:48:06'

            select CAST((select enddate-startdate from @tbl) as time) TimeDiff

            :)

            Niladri Biswas

            modified on Wednesday, October 28, 2009 5:56 AM

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Let me get this straight, you are casting a DATETIME to a DATETIME so you can cast it to TIME? :wtf: :omg:


            only two letters away from being an asset

            N 1 Reply Last reply
            0
            • N Not Active

              Let me get this straight, you are casting a DATETIME to a DATETIME so you can cast it to TIME? :wtf: :omg:


              only two letters away from being an asset

              N Offline
              N Offline
              Niladri_Biswas
              wrote on last edited by
              #6

              Yae.. I didnot notice that I made that mistake. Very sorry. But thanks. :)

              Niladri Biswas

              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