sql days difference

To find the days difference between 2 date:

DATEDIFF(day, From_Date, To_Date) AS DAYS_DIFF

Some more example from MSDN:

SELECT DATEDIFF(year, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(quarter, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(month, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(dayofyear, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(day, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(week, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(hour, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(minute, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(second, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

SELECT DATEDIFF(millisecond, ‘2005-12-31 23:59:59.9999999’

, ‘2006-01-01 00:00:00.0000000’);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s