|
SQL DATEDIFF Function Returns the number of date and time boundaries crossed between two dates
In this
tutorials, I will show how to use the SQL DATEDIFF Function
to calculate number of different dates you might need to use in your Sql
Query.
For SQL DateAdd Function, click here
DATEDIFF Syntax
DATEDIFF (DatePart , StartDate , EndDate )
date-part :
year | quarter | month | week | day | hour | minute | second | millisecond
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate ='2007-06-05'
SET @EndDate ='2007-08-05'
SELECT DATEDIFF(Year, @StartDate, @EndDate) AS NewDate
Return Value = 0 Year
SELECT DATEDIFF(quarter, @StartDate, @EndDate) AS NewDate
Return Value = 1 quarter
SELECT DATEDIFF(Month, @StartDate, @EndDate) AS NewDate
Return Value = 2 Month
SELECT DATEDIFF(dayofyear,@StartDate, @EndDate) AS NewDate
Return Value = 61 day
SELECT DATEDIFF(Day, @StartDate, @EndDate) AS NewDate
Return Value = 61 Day
SELECT DATEDIFF(Week, @StartDate, @EndDate) AS NewDate
Return Value = 9 Week
SELECT DATEDIFF(Hour, @StartDate, @EndDate) AS NewDate
Return Value = 1464 Hour
SELECT DATEDIFF(minute, @StartDate, @EndDate) AS NewDate
Return Value = 87840 minute
SELECT DATEDIFF(second, @StartDate, @EndDate) AS NewDate
Return Value = 5270400 second
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate ='2007-06-05'
SET @EndDate ='2007-06-06'
SELECT DATEDIFF(millisecond, @StartDate, @EndDate) AS NewDate
Return Value = 86400000 millisecond
powered by www.pHick1.com
|
|