If you want to get the difference between two dates in SQL Server then you need to make use of DATEDIFF function.
DECLARE @StartDate DATETIME
SET @StartDate = '2012-03-08 12:22:00.000'
Declare @EndDate DATETIME
SET @EndDate = '2012-03-12 11:32:00.000'
--To get only Hours
SELECT DATEDIFF(hh, @StartDate,@EndDate ) AS [Hours];
--To get the result in Minutes
SELECT CAST(DATEDIFF(ss, @StartDate, @EndDate) AS DECIMAL(10, 0)) / 60 AS [Minutes];
--To get both Hours and Minutes
SELECT DATEDIFF(hh, @StartDate, @EndDate) AS [Hours],
DATEDIFF(mi,DATEADD(hh,DATEDIFF(hh, @StartDate, @EndDate),@StartDate),@EndDate) AS [Minutes];
DECLARE @StartDate DATETIME
SET @StartDate = '2012-03-08 12:22:00.000'
Declare @EndDate DATETIME
SET @EndDate = '2012-03-12 11:32:00.000'
--To get only Hours
SELECT DATEDIFF(hh, @StartDate,@EndDate ) AS [Hours];
--To get the result in Minutes
SELECT CAST(DATEDIFF(ss, @StartDate, @EndDate) AS DECIMAL(10, 0)) / 60 AS [Minutes];
--To get both Hours and Minutes
SELECT DATEDIFF(hh, @StartDate, @EndDate) AS [Hours],
DATEDIFF(mi,DATEADD(hh,DATEDIFF(hh, @StartDate, @EndDate),@StartDate),@EndDate) AS [Minutes];
No comments:
Post a Comment