I am going to dig in and write another short article or tidbit 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 functions that take a datetime variable as input and return an integer as output. The DAY() function returns an integer from 1 to 31 for valid dates.
The examples below are sample calls to the function. The inputs puts are valid date, null value and a empty string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
-- -- Extact part of date as an integer -- -- Valid Day SELECT DAY(SYSDATETIME()) AS DAY_INT GO -- Null Value SELECT DAY(NULL) AS DAY_NULL GO -- Empty String SELECT DAY(' ') AS DAY_EMPTY GO |
The output from the test is listed below. It is surprising that an empty string returns a value of 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
output: DAY_INT ----------- 8 DAY_NULL ----------- NULL DAY_EMPTY ----------- 1 </span> |
To summarize, this function returns the day of the month from a datetime variable in integer format.