I am going to remain on course with writing 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 functions that take a datetime variable as input and return an integer as output. The MONTH() function returns an integer from 1 to 12 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 Month SELECT MONTH(SYSDATETIME()) AS MONTH_INT GO -- Null Value SELECT MONTH(NULL) AS MONTH_NULL GO -- Empty string SELECT MONTH(' ') AS MONTH_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 |
output: DAY_INT ----------- 8 DAY_NULL ----------- NULL DAY_EMPTY ----------- 1 |
To recap, this function returns the day of the month from a datetime variable in integer format.