SQL Server: End of Month Function

In Microsoft SQL Server there is a Transact-SQL function called EOMONTH that lets you find the last day of the month for a given date. You also have the option to include an offset value to add a number of months to the date that you pass in.

For example, if you want to get the last day of the current month, you can use:
EOMONTH(GETDATE())

If you want to get the last day of next month, you can pass in a second parameter of 1:
EOMONTH(GETDATE(),1)

As you might have already guessed, you can use a negative number if you want to subtract from the months. So if you're looking for the last day of the month from three months ago, you could enter:
EOMONTH(GETDATE(),-3)

For all of these examples you can swap in whatever date value you like in place of where I've used GETDATE().

0 comments:

Post a Comment