Get different in minutes between 2 Datetime

  • Hi,

    I've table and data as follow,

    declare @tAccount table

    (

    isLocked bit,

    LastLockoutDate datetime,

    currentDte dateTime

    )

    insert into @tAccount values('true','2014-04-26 16:11:25.337',getdate())

    Let's say, current data as follow,

    isLocked| LastLockoutDate| currentDte

    12014-04-26 16:11:25.3372014-04-27 01:45:15.053

    How to get different in minutes between currentDate and LastLockoutDate? Mean, currentDate - LastLockoutDate.

    Really need help

  • The DATEDIFF function is your friend here

    😎

    SELECT

    isLocked

    ,LastLockoutDate

    ,currentDte

    ,DATEDIFF(MINUTE,LastLockoutDate,currentDte) AS DIFF_MINUTE

    FROM @tAccount

    Result

    isLocked LastLockoutDate currentDte DIFF_MINUTE

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

    1 2014-04-26 16:11:25.337 2014-04-26 18:57:15.777 166

  • tq 😀

  • Note that it won't return complete minutes (or any other period) and will count for the times the period changes.

    In the following example both calculations return 1 and that is expected.

    SELECT DATEDIFF(MI, '20140426 01:00:59.997', '20140426 01:01:00.000'),

    DATEDIFF(MI, '20140426 01:00:00.000', '20140426 01:01:59.997')

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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