SDU Tools: StartOfMonth and EndOfMonth in T-SQL

In our free SDU Tools for developers and DBAs, we have a large number of useful functions. The topic for today is two simple ones: StartOfMonth and EndOfMonth.

SQL Server 2012 added a function EOMONTH. It returns the end of the month date for any given date. I really, really don't like abbreviations like this. There is no need to save three characters.

I asked why it wasn't ENDOFMONTH but was told that EOMONTH was the name of the function in Excel. It's sad to think that's the guiding principle for T-SQL function names. I also don't get why sometimes the words in the names are separated by underscores, and other times they aren't. Surely END_OF_MONTH would be more readable.

Also, there was no corresponding SOMONTH (START_OF_MONTH). I have no idea why.

So in SDU Tools, we've just added two simple functions that do both and have sensible names.

Even if you just want to use code inline, these will give you simple examples of how to reliably calculate these using T-SQL.

You can see these functions in action in the main image above, and in this video:

To become an SDU Insider and to get our free tools and eBooks, please just visit here:

http://sdutools.sqldownunder.com

 

Leave a Reply

Your email address will not be published.