Dofactory.com
Dofactory.com
Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

SQL MONTH Function

MONTH returns the month number (1-12) for a given date.

MONTH is equivalent to DATEPART(month, date).

Example

#

This example returns the month from the specified date.

SELECT MONTH('2022-12-18 11:54:07') AS Month
Result:  1 record
Month
12

Using MONTH

#

A common use is to get the current month.

SELECT MONTH(GETDATE()) as 'Current Month'
Result:  1 record
Current Month
12

Syntax

Syntax of the MONTH function .

MONTH(input_date) 

input_date -- a date or datetime value.


More Examples

MONTH in WHERE

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List all orders placed in September 2013.
SELECT FirstName, LastName, OrderNumber, 
       OrderDate, TotalAmount
  FROM [Order] O
  JOIN Customer C ON C.Id = O.CustomerId
 WHERE MONTH(OrderDate) = 9 AND 
       YEAR(OrderDate) = 2013
Result:  37 records
FirstName LastName OrderNumber OrderDate TotalAmount
Rita Müller 542781 2013-09-01 00:00:00.000 530.40
André Fonseca 542782 2013-09-01 00:00:00.000 331.78
Peter Franken 542783 2013-09-02 00:00:00.000 1203.50
Christina Berglund 542784 2013-09-02 00:00:00.000 668.70
Maurizio Moroni 542785 2013-09-03 00:00:00.000 193.00

MONTH in GROUP BY, ORDER BY

ORDER
Id
OrderDate
OrderNumber
CustomerId
TotalAmount
CUSTOMER
Id
FirstName
LastName
City
Country
Phone
Problem: List the number of orders by month for the year 2013.
SELECT MONTH(OrderDate) AS Month,
       COUNT(Id) AS Count 
  FROM [Order]
 WHERE YEAR(OrderDate) = 2013
 GROUP BY MONTH(OrderDate)
 ORDER BY MONTH(OrderDate)
Result:  12 records
Month Count
1 33
2 29
3 30
4 31
5 32
6 30
7 33
8 33
9 37
10 38
11 34
12 48

You may also like



Last updated on Dec 21, 2023

Earn income with your data and sql skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.