Month and Eomonth in SQL Server 2012

In this article, I would like to show the functions Month and Eomonth in SQL Server. The Month and Emonth are part of the datetime functions. The Month Function returns an integer that represents the month of the specified date and similarly the Eomonth function returns the last day of the month that contains the specified date. So let's have a look at a practical example of how to use the Month and Eomonth functions in SQL Server 2012. The example is developed in SQL Server 2012 using SQL Server Management Studio.

The Month Function

The Month Function returns an integer that represents the month of the specified date.

Syntax

The syntax of the Month built-in date function is as follows :

MONTH ( Date )

Here, The <Date> parameter can be an expression, column expression, user-defined variable, or string literal.

Example

Select  getdate() as CurrentDate

Go

Select month(getdate()) as Month

Go

Select month('09/12/2012') as Month

Output

Month-Function-in-SQL-Server.jpg

The Eomonth Function

The Eomonth function returns the last day of the month that contains the specified date.

Syntax

The syntax of the Month built-in date function is as follows :

MONTH ( startdate [,month_to_add ] )

Here,

The <Startdate> parameter can be an expression specifying the date for which to return the last day of the month.

The month_to_add is optional.

Example

Select  getdate() as CurrentDate

Go

Select Eomonth(getdate()) as Month

Go

Select Eomonth('09/12/2012',2) as Month

Go

Select Eomonth('09/12/2012') as Month

 

Output

Eomonth-Function-in-SQL-Server.jpg


Similar Articles