get the time differencial between two hours

Posted on 2011-04-20
Last Modified: 2012-06-27
hi there,
I have stored two dates  on my table and it looks like this:
event_time                        event_time_end
1900-01-01 10:20:00.000 1900-01-01 10:30:00.000

notes: this two hours are not always 15 minutes it varies hence the reason why i need to get the time difference between the two.
how can i get the difference between the two?
Question by:COHFL
    LVL 22

    Expert Comment

    by:Nico Bontenbal
    select DATEDIFF(MINUTE,'1900-01-01 10:20:00.000', '1900-01-01 10:30:00.000')

    Open in new window

    You could divide by 60 to get the hours like this:
    DATEDIFF(MINUTE,'1900-01-01 10:20:00.000', '1900-01-01 10:30:00.000')/convert(real,60)
    And you'll probably want to replace the time string with field names from your table. Something like:
    select DATEDIFF(MINUTE,event_time, event_time_end)/convert(real,60) from event_times
    LVL 21

    Expert Comment

    by:Alpesh Patel
    select datediff(minute, date1, date2)

    Author Comment

    the only problem with doing the division by 60 if i run this:
    select DATEDIFF(MINUTE,'2008-01-01 12:00:00', '2008-01-01 12:15:00')/convert(real,60)
    i get 0.25 where it should display 0.15
    LVL 22

    Accepted Solution

    Something like this than maybe:
    select dateadd(MINUTE, DATEDIFF(MINUTE,'1900-01-01 10:20:00.000', '1900-01-01 10:35:00.000'), '1900-01-01')
    returns the difference as a time. Will only work if the difference is less than a day.
    Or what about:
    select convert(varchar(3),DATEPART(HOUR, dateadd(HOUR, DATEDIFF(HOUR,'1900-01-01 09:20:00.000', '1900-01-01 10:35:00.000'), '1900-01-01'))) + ' hours and ' +  convert(varchar(3),DATEPART(MINUTE, dateadd(MINUTE, DATEDIFF(MINUTE,'1900-01-01 09:20:00.000', '1900-01-01 10:35:00.000'), '1900-01-01'))) + ' minutes'


    Author Comment

    ok that did it.
    iuse your suggestion:
    select dateadd(MINUTE, DATEDIFF(MINUTE,'1900-01-01 10:20:00.000', '1900-01-01 10:35:00.000'), '1900-01-01')

    and i add a small rinkle to filter out the time portion of it so it looks cleaner:

    select CONVERT(VARCHAR(8), dateadd(MINUTE, DATEDIFF(MINUTE,'1900-01-01 08:20:00.000', '1900-01-01 10:35:00.000'), '1900-01-01'), 108)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    This is my first ever Article on EE or elsewhere; therefore, please bear with me if I have some discrepancies in my writing. I read many articles and questions related to "how to pass values to SSIS packages at run-time?"  Hence, this common ques…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now