MONTH

Updated at:

The MONTH function extracts the month value (1 to 12) from the specified date value date.

Syntax

INT MONTH(DATE|DATETIME|TIMESTAMP|TIMESTAMP_NTZ|STRING|INTERVAL <date>)

-- Standard example.
-- Returns 6.
SELECT MONTH('2025-06-19');

Parameters

date: required. A value of the DATE, DATETIME, TIMESTAMP, TIMESTAMP_NTZ, STRING, or INTERVAL type.

  • If date is of the STRING type, the date format must contain yyyy-mm-dd.

  • If date is of the INTERVAL type, only INTERVAL_YEAR_MONTH is supported. INTERVAL_DAY_TIME is not supported. For more information, see INTERVAL data type.

Return value

The function returns a value of the INT type based on the following rules:

  • If date is not of the DATE, DATETIME, TIMESTAMP, TIMESTAMP_NTZ, STRING, or INTERVAL type, the function returns NULL or reports an error.

  • If date is NULL, the function returns NULL.

Examples

  • Example 1: Extract the month from a DATETIME value.

    -- Returns 3.
    SELECT MONTH(DATETIME '2025-03-05 12:45:10');
  • Example 2: Process a STRING input.

    -- Returns 8.
    SELECT MONTH('2025-08-16 09:20:30');
  • Example 3: Extract the month field from an INTERVAL YEAR TO MONTH value.

    -- Enable the MaxCompute V2.0 data type edition.
    SET odps.sql.type.system.odps2 = true;
    -- Returns 6.
    SELECT MONTH(INTERVAL '2-6' YEAR TO MONTH);
  • Example 4: Test boundary values and NULL handling.

    -- Returns NULL.
    SELECT MONTH(NULL);
    
    -- Test the minimum valid month.
    -- Returns 1.
    SELECT MONTH('2025-01-01');
    
    -- Test the maximum valid month.
    -- Returns 12.
    SELECT MONTH('2025-12-31 23:59:59');
  • Example 5: Process an invalid STRING input.

    -- The nonstandard date string cannot be parsed.
    -- Returns NULL.
    SELECT MONTH('20250305');

Related functions

MONTH is a date function. For more information about functions for date calculations and conversions, see Date functions.