MONTH() function is used to get month from the date provided in the MONTH() function. It gives the single MONTH result in positive integer between first month of the year to last month of the year.
General Syntax is as follows:
Get month from GETDATE() Function
We can get month from GETDATE() function which gives the month in integer from the current date.
SELECT MONTH(GETDATE()) AS MonthFromGetDate
Get month from date provided as string in single quotes
We can get month from date provided as string in single quotes which gives the month in integer.
SELECT MONTH('2022/01/31') AS MonthFromDate
Get month from table Column
We can get month from table having DATETIME data type. To get the month from the table column, first we create a table and insert some rows and then we will get the month from that particular column.
USE HR GO CREATE TABLE tblEmployee ( EmpID INT PRIMARY KEY NOT NULL, EmpName VARCHAR(20), EmpGender VARCHAR(10), EmpAge INT, EmpSalary INT, EmpJoiningDate DATETIME ) GO
Let’s insert some records into tblEmployee table.
USE HR GO INSERT INTO tblEmployee VALUES (1,'Simon','Male',25,25000,'2015-01-01 22:04:49.400'), (2,'Dave','Male',29,15000,'2016-03-25 20:04:55.400'), (3,'Sara','Female',35,20000,'2017-05-01 18:04:35.400'), (4,'Julia','Female',36,35000,'2018-01-10 15:03:45.400'), (5,'Sam','Male',32,35000,'2020-03-20 10:02:25.400') GO
Below is the result of records of table tblEmployee.
Now, below is the query to get month from the EmpJoiningDate column of the table tblEmployee.
USE HR GO SELECT EmpName, EmpGender, EmpSalary, MONTH(EmpJoiningDate) AS JoiningMonth FROM tblEmployee GO