November 14, 2011 at 1:50 pm
Hello All,
Can someone please help me with a sql query I am truing to find the diference of two datetime in hh(hour):M(minutes):ss(seconds) format.
ex: if Startdate is "2011-11-11 15:25:26.680" and end date is "2011-11-11 15:25:48.957". I need to find the difference of time say '00hr:00min:22sec' somethiing like this.
please help me..
thanks in advance
November 14, 2011 at 1:59 pm
something like this?
select [Years] = datediff(year,0,ET-ST)-1,
[Months] = datepart(month,ET-ST)-1,
[Days] = datepart(day,ET-ST)-1,
[Hours] = datepart(Hour,ET-ST),
[Minutes] = datepart(Minute,ET-ST),
[Seconds] = datepart(Second,ET-ST),
[Milliseconds] = datepart(millisecond,ET-ST)
from
(
select -- Test Data
ST = convert(datetime,'2008/09/22 00:35:33.997'),
ET = convert(datetime,'2009/10/23 04:05:45.443')
) a
Lowell
November 14, 2011 at 2:06 pm
Use DATEDIFF in seconds, then do some math. Note the use of integers for the division to keep it in whole numbers.
IE:
DECLARE @DT1 DATETIME,
@dt2 DATETIME
SELECT@DT1 = '20100101 10:30:45',
@dt2 = '20100101 12:28:39'
SELECT
DATEDIFF( ss, @DT1, @dt2) AS TotalSeconds,
DATEDIFF( ss, @DT1, @dt2)/ 60 AS TotalMinutes,
DATEDIFF( ss, @DT1, @dt2)/60/60 AS TotalHours,
DATEDIFF( ss, @DT1, @dt2)/60%60 AS LeftoverMinutes,
DATEDIFF( ss, @DT1, @dt2)%60 AS LeftoverSeconds,
CONVERT( VARCHAR(3), DATEDIFF( ss, @DT1, @dt2)/60/60) + 'hh:' +
RIGHT( '00' + CONVERT( VARCHAR(2), DATEDIFF( ss, @DT1, @dt2)/60%60), 2) + 'mm:' +
RIGHT( '00' + CONVERT( VARCHAR(2), DATEDIFF( ss, @DT1, @dt2)%60), 2) + 'ss'
EDIT: Sniped by Lowell. Hm, nice code there. Like the datepart trick.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
November 14, 2011 at 2:48 pm
Thanks for the reply guys
I got a simple way out for it
SELECT convert(varchar, EndDateTime- StartDateTime, 114) FROM Yourtablename
O/p will be :00:00:22:277
November 14, 2011 at 2:54 pm
learnsql84 (11/14/2011)
Thanks for the reply guysI got a simple way out for it
SELECT convert(varchar, EndDateTime- StartDateTime, 114) FROM Yourtablename
O/p will be :00:00:22:277
It works, but it won't catch more than 24 hours correctly. Look at the results of this:
DECLARE @DT1 DATETIME,
@dt2 DATETIME
SELECT@DT1 = '20100101 10:30:45',
@dt2 = '20100103 12:28:39'
SELECT convert(varchar, @dt2 - @DT1, 114)
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
November 14, 2011 at 3:18 pm
Lowell (11/14/2011)
something like this?
select [Years] = datediff(year,0,ET-ST)-1,
[Months] = datepart(month,ET-ST)-1,
[Days] = datepart(day,ET-ST)-1,
[Hours] = datepart(Hour,ET-ST),
[Minutes] = datepart(Minute,ET-ST),
[Seconds] = datepart(Second,ET-ST),
[Milliseconds] = datepart(millisecond,ET-ST)
from
(
select -- Test Data
ST = convert(datetime,'2008/09/22 00:35:33.997'),
ET = convert(datetime,'2009/10/23 04:05:45.443')
) a
I was under the impression that using '-' or '+' on the new datetime data types will no longer work. So, I tested the above and changed the data type to datetime2 and it did indeed fail.
I would caution using the above if you will be moving to the new date/time data types.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
November 15, 2011 at 2:02 am
Another way of doing it if you never have spans exceeding 24hr:
SELECT CAST(DATEADD(MILLISECOND, (DATEDIFF(MILLISECOND, '2011-11-11 15:25:26.680', '2011-11-11 15:25:48.957')),CAST(0 AS DATETIME) ) AS TIME)
There are probably simpler ways to do this 😀
November 15, 2011 at 12:04 pm
Thanks You all guys...U all are great help...
November 16, 2011 at 1:57 am
DECLARE @STARTDATE DateTime
SET @STARTDATE = '2011/11/11 15:25:26.680'
DECLARE @ENDDATE DateTime
SET @ENDDATE = '2011/11/11 15:25:48.957'
SELECT(DATEDIFF(d,@STARTDATE, @ENDDATE)) as DaysDiff,
(DATEDIFF(ww,@STARTDATE, @ENDDATE)) as WeeksDiff,
(DATEDIFF(hh,@STARTDATE, @ENDDATE)) as HoursDiff,
(DATEDIFF(MINUTE,@STARTDATE, @ENDDATE)) as MinDiff,
(DATEDIFF(SS,@STARTDATE, @ENDDATE)) AS SecDiff,
CAST(DATEDIFF(HH,@STARTDATE,@ENDDATE) AS VARCHAR) + ':' + CAST(DATEDIFF(MINUTE,@STARTDATE, @ENDDATE)%60 AS VARCHAR)
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply