I am unfluctuating in my creation of short articles or tidbits on Transaction SQL date/time functions.
Most, if not all, modern day computer systems collect data which contains dates and times. Many times you will want to calculate the difference between two given dates using a particular unit of measure (date part) like day, week, month, quarter or year.
Today, I will be exploring the DATEDIFF() function. This function has 12 different input parameters, units of measure (date part), that can be passed to change the resulting output.
To summarize, the function takes a date part, a start date/time variable and a end date/time variable as input returns the calculated difference in the correct unit of measure.
I will be exploring the six date part parameters that determine the units of measure
DATE PART | PARAMETER TO USE |
YEAR | YYYY or YY |
QUARTER | QQ or Q |
MONTH | MM or M |
DAY OF YEAR | DY or Y |
DAY | DD or D |
WEEK | WK or WW |
The examples below are sample calls to the function. The inputs puts are valid date part parameters and date time variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
-- -- Find the difference in date parts (unit of measure) -- -- Years DECLARE @DT1 DATETIME2 = '2013-12-10 12:32:10.12'; DECLARE @DT2 DATETIME2 = '2014-12-10 12:32:10.12'; SELECT DATEDIFF(YYYY, @DT1, @DT2) AS DIFF_YR1, DATEDIFF(YY, @DT1, @DT2) AS DIFF_YR2 GO -- Quarters DECLARE @DT1 DATETIME2 = '2013-12-10 12:32:10.12'; DECLARE @DT2 DATETIME2 = '2014-06-10 12:32:10.12'; SELECT DATEDIFF(QQ, @DT1, @DT2) AS DIFF_QTR1, DATEDIFF(Q, @DT1, @DT2) AS DIFF_QTR2 GO -- Months DECLARE @DT1 DATETIME2 = '2013-12-10 12:32:10.12'; DECLARE @DT2 DATETIME2 = '2014-06-10 12:32:10.12'; SELECT DATEDIFF(MM, @DT1, @DT2) AS DIFF_MM1, DATEDIFF(M, @DT1, @DT2) AS DIFF_MM2 GO -- Day of year (days) DECLARE @DT1 DATETIME2 = '2013-12-10 12:32:10.12'; DECLARE @DT2 DATETIME2 = '2014-06-10 12:32:10.12'; SELECT DATEDIFF(DY, @DT1, @DT2) AS DIFF_DOY1, DATEDIFF(Y, @DT1, @DT2) AS DIFF_DOY2 GO -- Days DECLARE @DT1 DATETIME2 = '2013-12-10 12:32:10.12'; DECLARE @DT2 DATETIME2 = '2014-06-10 12:32:10.12'; SELECT DATEDIFF(DD, @DT1, @DT2) AS DIFF_DD1, DATEDIFF(D, @DT1, @DT2) AS DIFF_DD2 GO -- Weeks DECLARE @DT1 DATETIME2 = '2013-12-10 12:32:10.12'; DECLARE @DT2 DATETIME2 = '2014-06-10 12:32:10.12'; SELECT DATEDIFF(WK, @DT1, @DT2) AS DIFF_WK1, DATEDIFF(WW, @DT1, @DT2) AS DIFF_WK2 GO |
The output from the test is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
output: DIFF_YR1 DIFF_YR2 ----------- ----------- 1 1 DIFF_QTR1 DIFF_QTR2 ----------- ----------- 2 2 DIFF_MM1 DIFF_MM2 ----------- ----------- 6 6 DIFF_DOY1 DIFF_DOY2 ----------- ----------- 182 182 DIFF_DD1 DIFF_DD2 ----------- ----------- 182 182 DIFF_WK1 DIFF_WK2 ----------- ----------- 26 26 < |
Like any good unit test, one should try both positive and negative test cases. I am not going to explore how an EMPTY string or NULL value effect the results provided by the function.
However, the following rules apply. Any invalid date parameter results in a raised error. Any NULL values passed as parameters result in a NULL result. Any EMPTY strings result in a default date.
To make a long story short, pass one of the six date part parameters with two date/time variables to calculated the difference. Next time, I will be talking about how to use this function with Time Related Parts.