SQL MONTH() Function

SQL MONTH() Function


The SQL MONTH() is a function, and return or extract the month value (a whole integer number from 1 to 12) of a given input date value.

The SQL MONTH() function is supports only date or datetime based table columns or fields.

It can be used in SELECT statement as well in where clause.



Sl month function using sql server month from date, current month, getdate month, datepart month, sql server last month.

SQL MONTH() Syntax

The below syntax is used to get month value from a given input date value using MONTH() function.

For SQL SERVER / MY SQL / MS ACCESS


SELECT MONTH('valid_date_value');

The below syntax is used to read month portion (a integer number from 1 to 12) value from a given table column using MONTH() function.

For MS ACCESS / SQL SERVER / MY SQL


SELECT MONTH(date_column_name1) FROM table_name;

SQL MONTH() Example - Using Expression Or Formula

The following SQL SELECT statement will extract the month value from a given input date value. We use now() function to get the input date value.


SELECT 
NOW() AS 'Current Date and Time', 
MONTH(NOW()) AS 'Month Value';

The result of above query is:

Current Date and Time Month Value
24-08-2012 19:37:30 8

Sample Database Table - BookOrder

BookID BookName OrderDate Qty DeliveryDate
1 SQL Server 2012 Black Book 12-05-2009 07:10:20 6 21-05-2009 08:23:03
2 Professional MySql 11-01-2007 02:52:12 18 15-02-2007 20:21:26
3 Data Analysis Using SQL 17-08-2009 19:51:55 15 26-09-2009 07:52:29
4 PHP And MySQL Bible 17-09-2001 19:39:05 7 21-10-2001 14:56:30

SQL MONTH() Example - With Table Column

The following SQL SELECT statement display the column "OrderDate" and "DeliveryDate" from the "BookOrder" table. The MONTH() function will extract the month value(a whole integer number from 1 to 12) from the column "OrderDate" and we will stored that value to a new column called "Order Month" and "Delivery Month".


SELECT 
OrderDate, MONTH(OrderDate) As 'Order Month', 
DeliveryDate, MONTH(DeliveryDate) As 'Delivery Month' 
FROM BookOrder;

The result of above query is:

OrderDate Order Month DeliveryDate Delivery Month
12-05-2009 07:10:20 5 21-05-2009 08:23:03 5
11-01-2007 02:52:12 1 15-02-2007 20:21:26 2
17-08-2009 19:51:55 8 26-09-2009 07:52:29 9
17-09-2001 19:39:05 9 21-10-2001 14:56:30 10


Sql server month using compare month, get month and year from date, extract month sql, datetime format, sql datetime functions, date functions with examples, date operations, sql date arithmetic.