SQL MONTH Function is one of the Date and Time Function, which will return an integer that represent the month part of a specified Date.
In this article we will show you, How to use the SQL Server Month function with practical example. For this example, We are going to use the below shown data
SQL MONTH Syntax
The basic syntax of the MONTH Function in SQL Server is as follows:
date: It can be an expression that returns the date, or you can use the date and time directly.
SQL MONTH Function Example 1
In this example, we will show you the possible ways to use the Month function
-- Example for SQL MONTH Function -- Testing Month Function with Random Date SELECT MONTH('07-19-2004') AS [Day Example] -- Testing Month Function with Random Date and Time SELECT MONTH('2015-11-24 12:29:44.513') AS [Day Example] -- Testing Month Function with Todays Date SELECT MONTH(GETDATE()) AS [Todays Date]
SQL MONTH Function Example 2
In this example, we are going to return the Month numbers from Hire Date in Employee table. To achieve this, we are using both the DATEPART Function, and Month function. This explains that, you can get the month number using both these functions.
-- Example for SQL MONTH Function USE [SQL Tutorial] GO 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]
From the above screenshot, you can observe that, the DATEPART function, and Month function are returning the same result. Remember, before 2012 people used the DATEPART function to extract the Month Number but in SQL Server 2012, Microsoft introduced this Month function.
Thank You for Visiting Our Blog