Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear

In my table I have two Column Start & End both Data Type is datetime.

I want to get the hour and time.

I used this one for hour

DATEDIFF(hour,Dr_TimeStart,Dr_TimeEnd)

I got the hour. But when the Start and End column data are less then hour means time, Start is 0:00 and End 0:15 then return hour is 0

that's why i used min

DATEDIFF(mi,Dr_TimeStart,Dr_TimeEnd)

then i used the case

case when DATEDIFF(hh, dbo.Trn_Daily3.Dr_TimeStart, dbo.Trn_Daily3.Dr_TimeEnd) > 0 then DATEDIFF(hh, dbo.Trn_Daily3.Dr_TimeStart, dbo.Trn_Daily3.Dr_TimeEnd) else DATEDIFF(mi, dbo.Trn_Daily3.Dr_TimeStart, dbo.Trn_Daily3.Dr_TimeEnd) / 60 end

What i want is

I want to show the hour and time

Please help on this

Thanks
Basit.
Posted

1 solution

Try like below,
SQL
DECLARE @StartDate DATETIME;
DECLARE @EndDate DATETIME;

SET @StartDate = '2015-11-18 00:00:16';

SET @EndDate = '2015-11-18 00:55:36';

select CONVERT(varchar(10), DATEDIFF(HOUR,@StartDate,@EndDate)) +':'+CONVERT(varchar(10), DATEDIFF(MINUTE,@StartDate,@EndDate)%60 )
 
Share this answer
 
Comments
basitsar 19-Nov-15 2:07am    
Many Thanks Dear, Perfect.
basitsar 19-Nov-15 2:11am    
Another thing dear, how to get the sum of that difference?

select CONVERT(varchar(10), DATEDIFF(HOUR,@StartDate,@EndDate)) +':'+CONVERT(varchar(10), DATEDIFF(MINUTE,@StartDate,@EndDate)%60 ) as DifferenceHour

Means how to get the sum of DifferenceHour column.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900