Using The TSQL DATEDIFF Function

Download PDF

Here is a quick video training on how to use the T-SQL DATEDIFF function on SQL Server. This was originally part of my free SQL query training for the 70-461 certification exam.

And here is the sample code to go with the DATEDIFF video tutorial:

SELECT DATEDIFF(week, '1/1/2015', GETDATE());
-- does the order matter?
SELECT DATEDIFF(week, GETDATE(), '1/1/2015');


SELECT DATEDIFF(year, '1/1/2015', GETDATE());
SELECT DATEDIFF(quarter, '1/1/2015', GETDATE());
SELECT DATEDIFF(month, '1/1/2015', GETDATE());
SELECT DATEDIFF(dayofyear, '1/1/2015', GETDATE());
SELECT DATEDIFF(day, '1/1/2015', GETDATE());
SELECT DATEDIFF(week, '1/1/2015', GETDATE());
SELECT DATEDIFF(hour, '1/1/2015', GETDATE());
SELECT DATEDIFF(minute, '1/1/2015', GETDATE());
SELECT DATEDIFF(second, '1/1/2015', GETDATE());
SELECT DATEDIFF(millisecond, '1/1/2015', GETDATE());
SELECT DATEDIFF(microsecond, '1/1/2015', GETDATE());

-- to help eliminate the overflow.
SELECT DATEDIFF(millisecond, '3/19/2015', GETDATE());
SELECT DATEDIFF(microsecond, '3/19/2015', GETDATE());

SELECT DATEDIFF(microsecond, '3/19/2015 11:00am', GETDATE());

See Also

 

More from Stedman Solutions:

SteveStedman5
Steve and the team at Stedman Solutions are here for all your SQL Server needs.
Contact us today for your free 30 minute consultation..
We are ready to help!

Leave a Reply

Your email address will not be published. Required fields are marked *

*