I am going to make progress with my series 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.
Not only is it important to know when a sales order came in, but we should fulfill the request using some type of queue (LIFO). If if was a stack (FIFO) implementation, the first person may never get his order!
Today, I will be exploring the DATEPART() function. This function has 15 different input parameters that can be passed to change the resulting output.
In short, the function takes a date part and date time variables as input returns the part that you want as an integer. I will be concentrating on how to extract the week (WK, WW) in this article.
The examples below are sample calls to the function. The inputs puts are valid date, null value and a empty string.
-- Functions to get date and time parts as INT
DATEPART(WK, SYSDATETIME()) AS WEEK_A_INT,
DATEPART(WW, SYSDATETIME()) AS WEEK_B_INT,
DATEPART(WK, NULL) AS WEEK_C_INT,
DATEPART(WW, '') AS WEEK_D_INT
The output from the test is listed below. It is surprising that an empty string returns a value of 1.
WEEK_A_INT WEEK_B_INT WEEK_C_INT WEEK_D_INT
----------- ----------- ----------- -----------
48 48 NULL 1
In as nutshell, pass one of the two date part parameters to extract the week as an integer from the date time variable.