|
The following
T-SQL database script in Query Editor demonstrates the calculation of difference between dates using the DATEDIFF function. DATEDIFF returns the (signed integer) count of the datepart (year, month, day, etc.) boundaries crossed between the start date and end date.
-- T-SQL environment control statement
USE [tempdb]
GO
-- T-SQL date difference queries
SELECT Years=DATEDIFF(year, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Quarters=DATEDIFF(quarter, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Months=DATEDIFF(month, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Days=DATEDIFF(dayofyear, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Days=DATEDIFF(day, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Weeks=DATEDIFF(week, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Hours=DATEDIFF(hour, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Minutes=DATEDIFF(minute, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Seconds=DATEDIFF(second, getdate(), '2011-01-01 00:00:00.0000000');
SELECT Milliseconds=DATEDIFF(millisecond, getdate(), dateadd(hh, 2,getdate()));
SELECT Nanoseconds=convert(int, DATEDIFF(nanosecond, getdate(), dateadd(second, 2,getdate())));
-- Date difference in HH:MM:SS format
select convert(varchar,dateadd(minute, datediff(minute, getdate(), dateadd(second, 10000, getdate())),'1900-01-01'),24)
|