MaxCompute SQL provides common date functions. You can select an appropriate date function based on your needs to complete date calculations and conversions. This topic describes the syntax, parameters, and examples of the date functions supported by MaxCompute SQL. It guides you through data development by using date functions.

Function Feature
DATEADD Modifies a date value based on datepart and delta.
DATE_ADD Determines the value of startdate based on delta.
DATEDIFF Calculates the difference between two date values based on the time unit specified by datepart.
DATEPART Returns a specified component of a date value.
DATETRUNC Truncates a date value based on the time unit specified by datepart.
FROM_UNIXTIME Converts a UNIX timestamp of the BIGINT type to a date value of the DATETIME type.
GETDATE Returns the current system time as a date value.
ISDATE Determines whether a date string can be converted into a date value in a specified format.
LASTDAY Returns the last day of the month in which the specified date value falls.
TO_DATE Converts a string to a date value in a specified format.
TO_CHAR Converts a date value to a string in a specified format.
UNIX_TIMESTAMP Converts a date value to a UNIX timestamp that is an integer.
WEEKDAY Returns a number that represents the day of the week in which the provided date falls.
WEEKOFYEAR Returns a number that represents the week of the year in which the provided date falls.
ADD_MONTHS Returns a date value that is obtained after a number of months are added to a specified date.
CURRENT_TIMESTAMP Returns the current timestamp.
DAY Returns the day in which the specified date value falls.
DAYOFMONTH Returns the day component of a date value.
EXTRACT Returns a specified component of a timestamp.
FROM_UTC_TIMESTAMP Converts a UTC timestamp to a timestamp for a specified time zone.
HOUR Returns the hour component of a date value.
LAST_DAY Returns the last day of the month in which the specified date value falls.
MINUTE Returns the minute component of a date value.
MONTH Returns the month in which the specified date value falls.
MONTHS_BETWEEN Returns the number of months between specified date values.
NEXT_DAY Returns the date of the first weekday that is later than a specified data value.
QUARTER Returns the quarter in which the specified date value falls.
SECOND Returns the second component of a date value.
YEAR Returns the year in which the specified date value falls.

DATEADD

  • Syntax
    datetime dateadd(date|datetime|timestamp <date>, bigint <delta>, string <datepart>)
  • Description

    Changes a date value based on datepart and delta that you specified. To change the current time to a date value based on a specified delta, you can use this function with the GETDATE function.

  • Parameters
    • date: required. A date value of the DATE, DATETIME, or TIMESTAMP type.

      If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned.

    • delta: required. The interval you want to add to or subtract from the specified component of a date value. The value of this parameter must be of the BIGINT type. If the value of delta is greater than 0, the date value is incremented. Otherwise, the date value is decremented.

      If the input value is of the STRING or DOUBLE type, it is implicitly converted into a value of the BIGINT type before calculation. If the input value is of another data type, an error is returned.

      Note
      • If you add or subtract the interval specified by delta at a date component, a carry or return at more significant date components may occur. The year, month, hour, minute, and second components are computed by using different numeral systems. The year component uses the base-10 numeral system. The month component uses the base-12 numeral system. The hour component uses the base-24 numeral system. The minute and second components use the base-60 numeral system.
      • If the DATEADD function adds the interval specified by delta to the month component of a date value and this operation does not cause an overflow on the day component, keep the value of the day component unchanged. Otherwise, set the value of the day component to the last day of the specified month.
    • datepart: required. The date part that you want to modify in the date value. The value is a constant of the STRING type. If the input value is in an invalid format or is not a constant of the STRING type, an error is returned.

      The value of this parameter is specified in compliance with the rules of conversions between the STRING and DATETIME types. The value yyyy indicates that the DATEADD function adds an interval to the year component of the date value. The value mm indicates that the DATEADD function adds an interval to the month component of the date value. The value dd indicates that the DATEADD function adds an interval to the day component of the date value. For more information about the rules of data type conversion, see Type conversion. The Extended Date/Time Format (EDTF) is also supported, such as -year, -month, -mon, -day, and -hour.

  • Return value

    A value of the DATETIME type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    • Example 1: common use of DATEADD
      -- The return value is 2005-03-01 00:00:00. After one day is added, the result is beyond the last day of February. The actual date value is the first day of March.
      select dateadd(datetime '2005-02-28 00:00:00', 1, 'dd');
      -- The return value is 2005-02-27 00:00:00. One day is subtracted.
      select dateadd(datetime '2005-02-28 00:00:00', -1, 'dd');
      -- The return value is 2006-10-28 00:00:00. After 20 months are added, the month overflows, and the year value increases by 1.
      select dateadd(datetime '2005-02-28 00:00:00', 20, 'mm');
      -- The return value is 2005-03-28 00:00:00.
      select dateadd(datetime '2005-02-28 00:00:00', 1, 'mm');
      -- The return value is 2005-02-28 00:00:00. February in 2005 has only 28 days. Therefore, the last day of February is returned.
      select dateadd(datetime '2005-01-29 00:00:00', 1, 'mm');
      -- The return value is 2005-02-28 00:00:00.
      select dateadd(datetime '2005-03-30 00:00:00', -1, 'mm');
      -- The return value is 2005-03-18.
      select dateadd(date '2005-02-18', 1, 'mm');
      -- The return value is 2005-03-18 00:00:00.0.
      select dateadd(timestamp '2005-02-18 00:00:00', 1, 'mm');
      -- If the current time is 2020-11-17 16:31:44, the return value is 2020-11-16 16:31:44.
      select dateadd(getdate(),-1,'dd');
    • Example 2: use of DATEADD in which a value of the DATETIME type is expressed as a constant
      In MaxCompute SQL statements, a value of the DATETIME type cannot be directly expressed as a constant. The following statement uses an invalid expression of a value of the DATETIME type:
      select dateadd(2005-03-30 00:00:00, -1, 'mm');
      To describe a constant of the DATETIME type, use a valid expression of a value of the DATETIME type in the following statement:
      -- Explicitly convert a constant of the STRING type into the DATETIME type. The return value is 2005-02-28 00:00:00.
      select dateadd(cast("2005-03-30 00:00:00" as datetime), -1, 'mm');

DATE_ADD

  • Syntax
    date date_add(date|timestamp|string <startdate>, bigint <delta>)
  • Description

    Determines the value of startdate based on delta. To change the current time to a date value based on a specified delta, you can use this function with the GETDATE function.

  • Parameters
    • startdate: required. The start date. The values of the DATE, DATETIME, and STRING type are supported. If the value is of another data type, an error is returned.

      If the input value is of the STRING type, it is implicitly converted into a value of the DATE type before calculation. The value of the STRING type is in the format of 'yyyy-mm-dd', for example, '2019-12-27'.

    • delta: required. The interval you want to add to or subtract from the specified component of a date value. The value of this parameter must be of the BIGINT type. If the input value is of another data type, an error is returned. If the value of delta is greater than 0, the date value is incremented. Otherwise, the date value is decremented.
  • Return value

    A value of the DATE type is returned.

  • Examples
    -- Enable the new data types introduced in MaxCompute V2.0. Commit this statement along with SQL statements.
    set odps.sql.type.system.odps2=true;
    -- The return value is 2005-03-01. After one day is added, the result is beyond the last day of February. The actual value is the first day of March.
    select date_add(datetime '2005-02-28 00:00:00', 1);
    -- The return value is 2005-02-27. One day is subtracted.
    select date_add(date '2005-02-28', -1);
    -- The return value is 2005-03-20.
    select date_add('2005-02-28 00:00:00', 20);
    -- If the current time is 2020-11-17 16:31:44, the return value is 2020-11-16.
    select date_add(getdate(),-1);

DATEDIFF

  • Syntax
    bigint datediff(date|datetime|timestamp <date1>, date|datetime|timestamp <date2>, string <datepart>)
  • Description

    Calculates the difference between date1 and date2. The difference is measured in the time unit specified by datepart.

  • Parameters
    • date1, date2: required. The minuend and subtrahend, which are of the DATE, DATETIME, or TIMESTAMP type. If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned.
    • datepart: optional. The time unit, which is a constant of the STRING type. If datepart is not in the specified format or is of another data type, an error is returned.

      If you enable the new data types of MaxCompute, datepart can be left unspecified. By default, day is used. The EDTF is also supported, such as -year, -month, -mon, -day, and -hour.

      Note This function omits the lower unit based on the time unit specified by datepart, and then calculates the result.
  • Return value

    A value of the BIGINT type is returned. If any input parameter is set to NULL, NULL is returned. If date1 is earlier than date2, a negative value is returned.

  • Examples
    --The start time is 2005-12-31 23:59:59 and the end time is 2006-01-01 00:00:00.
        -- The return value is 1.
        select datediff(end, start, 'dd'); 
        -- The return value is 1.
        select datediff(end, start, 'mm');
        -- The return value is 1.
        select datediff(end, start, 'yyyy');
        -- The return value is 1.
        select datediff(end, start, 'hh');
        -- The return value is 1.
        select datediff(end, start, 'mi');
        -- The return value is 1.
        select datediff(end, start, 'ss');
    -- The return value is 1800.
    select datediff(datetime'2013-05-31 13:00:00', '2013-05-31 12:30:00', 'ss');
    -- The return value is 30.
    select datediff(datetime'2013-05-31 13:00:00', '2013-05-31 12:30:00', 'mi');
    -- The return value is 11.
    select datediff(date '2013-05-21', date '2013-05-10', 'dd');
    -- The return value is 1800.
    select datediff(timestamp '2013-05-31 13:00:00', timestamp '2013-05-31 12:30:00', 'ss');
    -- The start time is 2018-06-04 19:33:23.234 and the end time is 2018-06-04 19:33:23.250. Date values with milliseconds do not adopt the standard DATETIME type and therefore cannot be implicitly converted into the DATETIME type. In this case, an explicit conversion is required. The return value is 16.
    select datediff(to_date('2018-06-04 19:33:23.250', 'yyyy-mm-dd hh:mi:ss.ff3'), to_date('2018-06-04 19:33:23.234', 'yyyy-mm-dd hh:mi:ss.ff3') , 'ff3');

DATEPART

  • Syntax
    bigint datepart(date|datetime|timestamp <date>, string <datepart>)
  • Description

    Returns a specified component of a date value.

  • Parameters
    • date: required. A value of the DATE, DATETIME, or TIMESTAMP type. If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned.
    • datepart: required. A constant of the STRING type. This parameter supports EDTF. If datepart is not in the specified format or is of another data type, an error is returned.
  • Return value

    A value of the BIGINT type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 2013.
    select datepart(datetime'2013-06-08 01:10:00', 'yyyy'); 
    -- The return value is 6.
    select datepart(datetime'2013-06-08 01:10:00', 'mm');
    -- The return value is 2013.
    select datepart(date '2013-06-08', 'yyyy');
    -- The return value is 2013.
    select datepart(timestamp '2013-06-08 01:10:00', 'yyyy');

DATETRUNC

  • Syntax
    datetime datetrunc (date|datetime|timestamp <date>, string <datepart>)
  • Description

    Truncates a date value based on the time unit specified by datepart.

  • Parameters
    • date: required. A value of the DATE, DATETIME, or TIMESTAMP type. If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned.
    • datepart: required. A constant of the STRING type. This parameter supports EDTF. If datepart is not in the specified format or is of another data type, an error is returned.
  • Return value

    A value of the DATETIME type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 2011-01-01 00:00:00.
    select datetrunc(datetime'2011-12-07 16:28:46', 'yyyy');
    -- The return value is 2011-12-01 00:00:00.
    select datetrunc(datetime'2011-12-07 16:28:46', 'month');
    -- The return value is 2011-12-07 00:00:00.
    select datetrunc(datetime'2011-12-07 16:28:46', 'DD');
    -- The return value is 2011-01-01.
    select datetrunc(date '2011-12-07', 'yyyy');
    -- The return value is 2011-01-01 00:00:00.0.
    select datetrunc(timestamp '2011-12-07 16:28:46', 'yyyy');

FROM_UNIXTIME

  • Syntax
    datetime from_unixtime(bigint <unixtime>)
  • Description

    Converts unixtime of the BIGINT type to a date value of the DATETIME type.

  • Parameters

    unixtime: required. A date value of the BIGINT type in the UNIX format. The value of this parameter is accurate to seconds. If the input value is of the STRING, DOUBLE, or DECIMAL type, it is implicitly converted into a value of the BIGINT type before calculation.

  • Return value
    A value of the DATETIME type is returned. If any input parameter is set to NULL, NULL is returned.
    Note In Hive-compatible mode where set odps.sql.hive.compatible=true; is executed, if the input value is of the STRING type, a date value of the STRING type is returned.
  • Examples
    -- The return value is 1973-11-30 05:33:09.
    select from_unixtime(123456789);

GETDATE

  • Syntax
    datetime getdate()
  • Description

    Returns the current system time as a date value. MaxCompute uses UTC+8 as the standard time zone.

  • Return value

    The current date and time are returned, which are of the DATETIME type.

    Note In MaxCompute SQL, getdate always returns a fixed value. The return value is an arbitrary time during the execution of the MaxCompute SQL task. The time is accurate to seconds. If you enable the new data types supported by MaxCompute V2.0, the time is accurate to milliseconds.

ISDATE

  • Syntax
    boolean isdate(string <date>, string <format>)
  • Description

    Determines whether a date string can be converted into a date value in a specified format. If the date string can be converted into a date value in the specified format, True is returned. Otherwise, False is returned.

  • Parameters
    • date: required. A value of the STRING type. If the input value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, it is implicitly converted into a value of the STRING type before calculation. If the input value is of another data type, an error is returned.
    • format: required. A constant of the STRING type. This parameter does not support EDTF. If the input value is of another data type, an error is returned. If redundant format strings exist in format, this function converts the date string that corresponds to the first format string into a date value. The rest strings are considered delimiters. For example, isdate("1234-yyyy", "yyyy-yyyy") returns True.
  • Return value

    A value of the BOOLEAN type is returned. If any input parameter is set to NULL, NULL is returned.

LASTDAY

  • Syntax
    datetime lastday(datetime <date>)
  • Description

    Returns the last day of the month in which the specified date value falls. The value is accurate to days. The hour, minute, and second components are expressed as 00:00:00.

  • Parameters

    date: a value of the DATETIME type. If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned.

  • Return value

    A value of the DATETIME type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 2013-06-30 00:00:00.
    select lastday (datetime '2013-06-08 01:10:00');

TO_DATE

  • Syntax
    datetime to_date(string <date>, string <format>)
  • Description

    Converts a string to a date value in a specified format.

  • Parameters
    • date: required. A date value of the STRING type. This parameter indicates the date string you want to convert. If the input value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, it is implicitly converted into a value of the STRING type before calculation. If the input value is of another data type or an empty string, an error is returned.
    • format: required. A constant of the STRING type. This parameter indicates a date format. If the input value is of another data type, an error is returned. format does not support EDTF. Other characters are omitted as invalid characters during parsing.

      The value of format must contain yyyy. Otherwise, an error is returned. If redundant format strings exist in format, this function converts the date string that corresponds to the first format string into a date value. The rest strings are considered delimiters. For example, to_date("1234-2234", "yyyy-yyyy") returns 1234-01-01 00:00:00.

      The format consists of different date components. yyyy indicates a 4-digit year. mm indicates a 2-digit month. dd indicates a 2-digit day. hh indicates an hour based on the base-24 numeral system. mi indicates a 2-digit minute. ss indicates a 2-digit second. ff3 indicates a 3-digit millisecond.

  • Return value

    A value of the DATETIME type is returned, which is in the format of yyyy-mm-dd hh:mi:ss. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 2010-12-03 00:00:00.
    select to_date('Alibaba 2010-12*03', 'Alibaba yyyy-mm*dd')
    -- The return value is 2008-07-18 00:00:00.
    select to_date('20080718', 'yyyymmdd');
    -- The return value is 2008-07-18 20:30:00.
    select to_date('200807182030','yyyymmddhhmi');
    -- '2008718' cannot be converted into a standard date value, and an error is returned. It must be written as '20080718'.
    select to_date('2008718', 'yyyymmdd');
    'Alibaba 2010-12*3' cannot be converted into a standard date value, and an error is returned. It must be written as 'Alibaba 2010-12*03'.
    select to_date('Alibaba 2010-12*3', 'Alibaba yyyy-mm*dd')
    '2010-24-01' cannot be converted into a standard date value, and an error is returned. It must be written as '2010-01-24'.
    select to_date('2010-24-01', 'yyyy');
    -- The return value is 2018-10-30 15:13:12.
    select to_date('20181030 15-13-12.345','yyyymmdd hh-mi-ss.ff3');

TO_CHAR

  • Syntax
    string to_char(datetime <date>, string <format>)
  • Description

    Converts a date value of the DATETIME type into a string in a specified format.

  • Parameters
    • date: required. A date value of the DATETIME type. This parameter indicates the date value you want to convert. If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned.
    • format: required. A constant of the STRING type. If the value is not a constant or not of the STRING type, an error is returned. In the format parameter, the date format part is replaced by the related data and the other characters remain unchanged in the output.
  • Return value

    A value of the STRING type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 'Alibaba Cloud Financial Services 2010-12*03'. Note that Chinese characters may not be properly displayed in the returned result if the MaxCompute client runs in Windows.
    select to_char(datetime'2010-12-03 00:00:00', 'Alibaba Cloud Financial Services yyyy-mm*dd');
    -- The return value is 20080718.
    select to_char(datetime'2008-07-18 00:00:00', 'yyyymmdd');
    -- 'Alibaba 2010-12*3' cannot be converted into a standard date value, and an error is returned. It must be written as 'Alibaba 2010-12*03'.
    select to_char(datetime'Alibaba 2010-12*3', 'Alibaba yyyy-mm*dd');
    -- '2010-24-01' cannot be converted into a standard date value, and an error is returned. It must be written as '2010-01-24'.
    select to_char(datetime'2010-24-01', 'yyyy');
    -- '2008718' cannot be converted into a standard date value, and an error is returned. It must be written as '20080718'.
    select to_char(datetime'2008718', 'yyyymmdd');

UNIX_TIMESTAMP

  • Syntax
    bigint unix_timestamp(datetime <date>)
  • Description

    Converts a date value to a UNIX timestamp that is an integer.

  • Parameters

    date: required. A value of the DATETIME type. If the input value is of the STRING type, it is implicitly converted into a value of the DATETIME type before calculation. If the input value is of another data type, an error is returned. If you enable the new data types, the implicit conversion fails. In this case, you must use the CAST function to convert the data type, for example, unix_timestamp(cast(... as datetime)).

  • Return value

    A UNIX timestamp of the BIGINT type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 1237518660.
    select unix_timestamp(datetime'2009-03-20 11:11:00'); 

WEEKDAY

  • Syntax
    bigint weekday (datetime <date>)
  • Description

    Returns a number that represents the day of the week in which the provided date falls.

  • Parameters

    date: required. A value of the DATETIME type.

  • Return value

    A value of the BIGINT type is returned. If any input parameter is set to NULL, NULL is returned. Monday is considered the first day of the week and its return value is 0. Days of the week are numbered in ascending order starting from 0. The return value of Sunday is 6.

  • Examples
    -- The return value is 4.
    select weekday (datetime '2009-03-20 11:11:00');

WEEKOFYEAR

  • Syntax
    bigint weekofyear (datetime <date>)
  • Description
    Returns a number that represents the week of the year in which the provided date falls. Monday is considered the first day of the week.
    Note To determine whether a week belongs to the current year or the next year, find the year in which more than four days of the week fall. If the week belongs to the year, it is considered the last week of the year. If the week belongs to the next year, it is considered the first week of the next year.
  • Parameters

    date: required. A value of the DATETIME type.

  • Return value

    A value of the BIGINT type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 1. -- 20141229 is in year 2014, but most days of the week fall in year 2015. Therefore, the return value 1 indicates the first week of year 2015.
    select weekofyear(to_date("20141229", "yyyymmdd"));  
    -- The return value is 1.    
    select weekofyear(to_date("20141231", "yyyymmdd")); 
    -- The return value is 53. 
    select weekofyear(to_date("20151229", "yyyymmdd"));

Additional functions of MaxCompute V2.0

MaxCompute V2.0 provides additional functions. If the functions you use involve new data types, execute the following SET statement to enable these data types. The new data types include TINYINT, SMALLINT, INT, FLOAT, VARCHAR, TIMESTAMP, and BINARY.
  • Session level: To use a new data type, you must insert set odps.sql.type.system.odps2=true; before the SQL statement, and commit and execute them together.
  • Project level: The project owner can configure new data types for the project as required. It takes 10 to 15 minutes for the settings to take effect. Run the following command:
    setproject odps.sql.type.system.odps2=true;
    For more information about setproject, see Project operations. For the precautions you must take when you enable data types at the project level, see Data types.

ADD_MONTHS

  • Syntax
    string add_months(string <startdate>, int <num_months>)
  • Description

    Returns a date value that is obtained after num_months are added to startdate. This function is an additional function of MaxCompute V2.0.

  • Parameters
    • startdate: required. A value of the STRING type. The format of the date value must include at least yyyy-mm-dd. Otherwise, NULL is returned.
    • num_months: required. A value of the INT type.
  • Return value

    A value of the STRING type is returned, which is in the format of yyyy-mm-dd.

  • Examples
    -- The return value is 2017-05-14.
    select add_months('2017-02-14',3);
    -- The return value is 0017-05-14.
    select add_months('17-2-14',3);
    -- The return value is 2017-05-14.
    select add_months('2017-02-14 21:30:00',3);
    -- The return value is NULL.
    select add_months('20170214',3);

CURRENT_TIMESTAMP

  • Syntax
    timestamp current_timestamp()
  • Description

    Returns the current timestamp. The return value is not fixed. This function is an additional function of MaxCompute V2.0.

  • Return value

    A value of the TIMESTAMP type is returned.

  • Examples
    -- The return value is '2017-08-03 11:50:30.661'.
    select current_timestamp(); 

DAY

  • Syntax
    int day(datetime|string <date>)
  • Description

    Returns the day in which the specified date value falls. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value can be yyyy-mm-dd or yyyy-mm-dd hh:mi:ss. If the date value is of another data type, NULL is returned.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 1.
    select day('2014-09-01');
    -- The return value is NULL.
    select day('20140901');

DAYOFMONTH

  • Syntax
    int dayofmonth(<datetime|string date>)
  • Description

    Returns the day component of a date value. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value can be yyyy-mm-dd or yyyy-mm-dd hh:mi:ss. If the date value is of another data type, NULL is returned.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 1.
    select dayofmonth('2014-09-01');
    -- The return value is NULL.
    select dayofmonth('20140901');

EXTRACT

  • Syntax
    int extract(<datepart> from <timestamp>)
  • Description

    Extracts the date component specified by datepart from the date timestamp. This function is an additional function of MaxCompute V2.0.

  • Parameters
    • datepart: required. A value that can be set to YEAR, MONTH, DAY, HOUR, or MINUTE.
    • timestamp: required. A value of the TIMESTAMP type.
  • Return value

    A value of the INT type is returned.

  • Examples
    set odps.sql.type.system.odps2=true;
    select  extract(year from '2019-05-01 11:21:00') year
             ,extract(month from '2019-05-01 11:21:00') month
             ,extract(day from '2019-05-01 11:21:00') day
             ,extract(hour from '2019-05-01 11:21:00') hour
             ,extract(minute from '2019-05-01 11:21:00') minute;
    
    -- Return result:
    +------+-------+------+------+--------+
    | year | month | day  | hour | minute |
    +------+-------+------+------+--------+
    | 2019 | 5     | 1    | 11   | 21     |
    +------+-------+------+------+--------+

FROM_UTC_TIMESTAMP

  • Syntax
    timestamp from_utc_timestamp({any primitive type}*, string <timezone>)
  • Description

    Converts a UTC timestamp to a timestamp for a specified time zone. This function is an additional function of MaxCompute V2.0.

  • Parameters
    • {any primitive type}*: required. A timestamp of the TIMESTAMP, DATETIME, TINYINT, SMALLINT, INT, or BIGINT type. If the value is of the TINYINT, SMALLINT, INT, or BIGINT type, the unit is milliseconds.
    • timezone: required. The destination time zone into which you want to convert the current time zone. For example, you can set this parameter to PST.

      This function supports only the Asia/Shanghai time zone and does not support the GMT+9 time zone.

      The UTC time refers to the time in the UTC+0 time zone. Shanghai, China is in the UTC+8 time zone.

  • Return value

    A value of the TIMESTAMP type is returned.

  • Examples
    -- The unit of the input value is milliseconds and the return value is 2017-08-01 04:24:00.0.
    select from_utc_timestamp(1501557840000, 'PST'); 
    -- The return value is 1970-01-30 08:00:00.0.
    select from_utc_timestamp('1970-01-30 16:00:00','PST'); 
    -- The return value is 1970-01-29 16:00:00.0.
    select from_utc_timestamp('1970-01-30','PST') ; 

HOUR

  • Syntax
    int hour(datetime|string <date>)
  • Description

    Returns the hour component of a date value.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value can be yyyy-mm-dd or yyyy-mm-dd hh:mi:ss. If the date value is of another data type, NULL is returned. This function is an additional function of MaxCompute V2.0.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 12.
    select hour('2014-09-01 12:00:00');
    -- The return value is 12.
    select hour('12:00:00');
    -- The return value is NULL.
    select hour('20140901120000');

LAST_DAY

  • Syntax
    string last_day(string <date>)
  • Description

    Returns the last day of the month in which the specified date value falls. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A value of the STRING type. The format of the date value must include at least yyyy-mm-dd. Otherwise, NULL is returned.

  • Return value

    A value of the STRING type is returned, which is in the format of yyyy-mm-dd.

  • Examples
    -- The return value is 2017-03-31.
    select last_day('2017-03-04');
    -- The return value is 2017-07-31.
    select last_day('2017-07-04 11:40:00');
    -- The return value is NULL.
    select last_day('20170304');

MINUTE

  • Syntax
    int minute(datetime|string <date>)
  • Description

    Returns the minute component of a date value. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value can be yyyy-mm-dd or yyyy-mm-dd hh:mi:ss. If the date value is of another data type, NULL is returned.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 30.
    select minute('2014-09-01 12:30:00'); 
    -- The return value is 30.
    select minute('12:30:00');
    -- The return value is NULL.
    select minute('20140901120000');

MONTH

  • Syntax
    int month(datetime|string <date>)
  • Description

    Returns the month in which the specified date value falls. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value can be yyyy-mm-dd or yyyy-mm-dd hh:mi:ss. If the date value is of another data type, NULL is returned.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 9.
    select month('2014-09-01');
    -- The return value is NULL.
    select month('20140901');

MONTHS_BETWEEN

  • Syntax
    double months_between(datetime|timestamp|string <date1>, datetime|timestamp|string <date2>)
  • Description

    Returns the number of months between date1 and date2. This function is an additional function of MaxCompute V2.0.

  • Parameters
    • date1: required. A date value of the DATETIME, TIMESTAMP, or STRING type and in the format of yyyy-mm-dd hh:mi:ss or yyyy-mm-dd.
    • date2: required. A date value of the DATETIME, TIMESTAMP, or STRING type and in the format of yyyy-mm-dd hh:mi:ss or yyyy-mm-dd.
  • Return value
    A value of the DOUBLE type is returned.
    • If date1 is later than date2, a positive value is returned. If date2 is later than date1, a negative value is returned.
    • If both date1 and date2 correspond to the last days of two months, the return value is an integer that represents the number of months. Otherwise, the return value is computed by using the following formula: (date1 - date2)/31.
  • Examples
    -- The return value is 3.9495967741935485.
    select months_between('1997-02-28 10:30:00', '1996-10-30');
    -- The return value is -3.9495967741935485.
    select months_between('1996-10-30','1997-02-28 10:30:00' );
    -- The return value is -3.0.
    select months_between('1996-09-30','1996-12-31');

NEXT_DAY

  • Syntax
    string next_day(string <startdate>, string <week>)
  • Description

    Returns the date of the first day that is later than startdate and matches the week value. The date of the specified day in the next week is returned. This function is an additional function of MaxCompute V2.0.

  • Parameters
    • startdate: required. A value of the STRING type. The format of the date value must include at least yyyy-mm-dd. Otherwise, NULL is returned.
    • week: required. A value of the STRING type. The value of this parameter can be the first two or three letters or the full name of a weekday, for example, MO, TUE, or FRIDAY.
  • Return value

    A value of the STRING type is returned, which is in the format of yyyy-mm-dd.

  • Examples
    -- The return value is 2017-08-08.
    select next_day('2017-08-01','TU');
    -- The return value is 2017-08-08.
    select next_day('2017-08-01 23:34:00','TU');
    -- The return value is NULL.
    select next_day('20170801','TU');

QUARTER

  • Syntax
    int quarter (datetime|timestamp|string <date>)
  • Description

    Returns the quarter in which the specified date value falls. Valid values: 1 to 4. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A date value of the DATETIME, TIMESTAMP, or STRING type. The format of the date value must include yyyy-mm-dd. If the date value is of another data type, NULL is returned.

  • Return value

    A value of the INT type is returned. If any input parameter is set to NULL, NULL is returned.

  • Examples
    -- The return value is 4.
    select quarter('1970-11-12 10:00:00');
    -- The return value is 4.
    select quarter('1970-11-12');

SECOND

  • Syntax
    int second(datetime|string <date>)
  • Description

    Returns the second component of a date value.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value can be yyyy-mm-dd or yyyy-mm-dd hh:mi:ss. If the date value is of another data type, NULL is returned. This function is an additional function of MaxCompute V2.0.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 45.
    select second('2014-09-01 12:30:45');
    -- The return value is 45.
    select second('12:30:45');
    -- The return value is NULL.
    select second('20140901123045');

YEAR

  • Syntax
    int year(datetime|string <date>)
  • Description

    Returns the year in which the specified date value falls. This function is an additional function of MaxCompute V2.0.

  • Parameters

    date: required. A date value of the DATETIME or STRING type. The format of the date value must include yyyy-mm-dd and exclude redundant strings. Otherwise, NULL is returned.

  • Return value

    A value of the INT type is returned.

  • Examples
    -- The return value is 1970.
    select year('1970-01-01 12:30:00');
    -- The return value is 1970.
    select year('1970-01-01');
    -- The return value is 70.
    select year('70-01-01');
    -- The return value is NULL.
    select year('1970/03/09'); 
    -- The return value is NULL.
    select year(null);