SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get different in minutes between 2 Datetime


Get different in minutes between 2 Datetime

Author
Message
Qira
Qira
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 734
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
1 2014-04-26 16:11:25.337 2014-04-27 01:45:15.053



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

Really need help
Eirikur Eiriksson
Eirikur Eiriksson
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14920 Visits: 18591
The DATEDIFF function is your friend here
Cool
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

Qira
Qira
SSC-Enthusiastic
SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)SSC-Enthusiastic (130 reputation)

Group: General Forum Members
Points: 130 Visits: 734
tq :-D
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16329 Visits: 19069
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search