SQL MONTH Function

SQL Server MONTH Function will return an integer that represents the month part of a specified date and its syntax is

MONTH(date)

For this SQL Month Function example, we use the below-shown data

Employee Table 1

SQL MONTH Function Example

In this example, we will show you the possible ways to use the Month.

SELECT MONTH('07-19-2004') AS [Day Example]

-- Testing with RandomTime 
SELECT MONTH('2015-11-24 12:29:44.513') AS [Day Example]

-- Testing with Today
SELECT MONTH(GETDATE()) AS [Todays Date]
SQL MONTH Function Example 2

MONTH Function Example 2

In this case, we will return the Month numbers from Hire Date in the Employee table. To achieve this, we are using both the DATEPART and Month functions. It demonstrates that you can get the month number using both of these methods.

SELECT [EmpID]
      ,[FirstName] + ' '+ [LastName] AS [Full Name]
      ,[Occupation]
      ,[YearlyIncome]
      ,[HireDate]
      ,MONTH([HireDate]) AS [Month Number]
      ,DATEPART(month, [HireDate]) AS [This Month From DatePart]
  FROM [Employee]
SQL MONTH Function 3

As you can see that the DATEPART and Month methods return the same month result. Remember, before 2012, people used the DATEPART to extract the Month Number. Please refer to the DateTime method in Server for the Remaining methods.

Categories SQL