How do we determine the last day of a month?
We can determine The current date using CURRENT_TIMESTAMP function:
To find out the last day of the previous month, we can subtract day number from current date, i.e. the number of days from beginning of the month:
Now we need to add one month to current date beforehand:
Lastly, let's eliminate a time component from the result obtained above:
Since SQL Server 2012, EOMONTH function is available. This function allows us to obtain the same result without "procedural" logic:
If an error occurs when running above query, this means that tutorial is working with SQL Server version under 2012, and EOMONTH function is not supported.
Well, when I wrote this query, the following result was obtained
As we could see, OFMONTH function has date-type parameter. Moreover, there is second optional parameter which presents the number of months to be added, if any, to the first parameter. For example, the following query gives the latest days of previous month, current month and next month for the date '2016-01-28' respectively: