Thursday, December 23, 2010

T-SQL: How to get the difference in days between two dates

This is how you can get the difference in days between two dates in T-SQL:


declare @Today datetime
declare @NewYear datetime

set @Today = '2010/12/23';
set @NewYear = '2010/12/31';

select DATEDIFF(day, @today, @NewYear) as DifferenceInDays; 
-- Returns 8.