Date Difference

  • Hi all,

    How can I get the difference between two DATE columns! I tried DATEDIFF but it is giving either all hours or all minutes. I tried it to get the difference between      [ '2005-05-16 15:02:33.347'], [2005-05-17 09:23:30.813'] but the output was like 18hrs  or  1101 mins or  66057 secs. Can somebody help me out to get hours and mins;I dont care abt the secs.

  • declare @x int

    declare @hr int

    declare @min-2 int

    select @x = datediff(mi,'2005-05-16 15:02:33.347', '2005-05-17 09:23:30.813')

    select @hr = @x / 60

    select @min-2 = @x % 60

    select @hr as 'hours', @min-2 as 'minutes'

    go

    You can only get one type of value back.  You have to do the math


    And then again, I might be wrong ...
    David Webb

  • If you can guarantee that the values will fall within the same day, then you could try

    select Convert(varchar(5),Cast('2005-05-16 16:02:33.347' as datetime)- Cast('2005-05-17 09:23:30.813'as datetime),108)

  • It's not quite true... you can get only 1 value at the time, but you can use datediff to get the hours and then recall it to get the minutes... Might run slower than David's solution though.

  • FWIW, another one:

    select

     convert(char(8),dateadd(ss,datediff(mi,'2005-05-16 15:02:33.347', '2005-05-17 09:23:30.813') ,0),108)

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Nice one Frank... it's nice to see you back here. Where have you been?

  • Thanks, I've never been away.

    Just reducing activity to work, learn and read.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • It worked me great........Thanks

  • I see Frank... Looks like I'm gonna get to 10k posts before you do at your current pace.

  • Do you really want to bet on this?

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Too much trouble to go to Europe to get your check...

  • Since I started my statistics, my avg. postcount daily is about ~8, since I record yours, yours is ~6,32. Guess when both counts will intersect

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • How about you recount that for the last month?

  • Ever had a beginners course in statistics? Results should be too volatile...

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Don't need a stats course to understand this >>

    as of last Friday :

    Select 5222.0 / datediff(d, '12/5/2002', getdate()) as FRANK, 138 as POSTS_MAY_FRANK, 1369.0 / datediff(d, '5/10/2004', getdate()) as REMI, 368 as POSTS_MAY_REMI

    LifetTime_FRANK POSTS_MAY_FRANK LifetTime_REMI POSTS_MAY_REMI

    ------------------ --------------- ------------------ --------------

    5.757442116868 138 3.555844155844 368

    You got a better lifetime average but you're far off my current pace so it's actually possible that I catch up to you eventually.

Viewing 15 posts - 1 through 15 (of 20 total)

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