Getting the Last Date of the Current Month in SQL Server

In SQL Server, you can get the last date of the current month using the `EOMONTH` function. Here's how to do it.

Syntax

The syntax to get the last date of the current month is as follows:

SELECT EOMONTH(GETDATE()) AS LastDateOfMonth; 

This query returns the last date of the current month.

Example

Let's say we want to get the last date of the current month:

SELECT EOMONTH(GETDATE()) AS LastDateOfMonth; 

This query will return the result:

LastDateOfMonth
2023-03-31

The result shows that the last date of the current month (March 2023) is '2023-03-31'.

Alternative Syntax

You can also use the following alternative syntax to get the last date of the current month:

SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0) - 1 AS LastDateOfMonth; 

This syntax uses the `DATEADD` function to add the difference between the current month and the base date (0) to the base date, and then subtracts 1 to get the last date of the month.

Using a Variable

If you want to store the last date of the current month in a variable, you can use the following syntax:

DECLARE @LastDateOfMonth DATE; SET @LastDateOfMonth = EOMONTH(GETDATE()); SELECT @LastDateOfMonth; 

This query declares a variable `@LastDateOfMonth` and sets it to the last date of the current month using the `EOMONTH` function.