OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V2.2.77Enterprise Edition

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V2.2.77
    iconOceanBase Database
    SQL - V 2.2.77Enterprise Edition
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    INTERVAL YEAR TO MONTH data type

    Last Updated:2023-08-18 09:26:34  Updated
    Share
    What is on this page
    Syntax
    Parameter
    Date formats
    Examples
    Calculations between interval and other date and time types

    folded

    Share

    The INTERVAL YEAR TO MONTH data type stores different objects from the DATE and TIMESTAMP data types. The TIMESTAMP data type stores specific timestamps. The DATE data type stores specific dates. The INTERVAL YEAR TO MONTH data type uses the YEAR and MONTH elements to store a period of time. This data type can be used to indicate the difference between two date and time values.

    Syntax

    INTERVAL YEAR [(percision)] TO MONTH
    

    Parameter

    Parameter
    Value
    Description
    percision 0~9 The precision in the YEAR element. The default value is 2. That is, if you do not specify this parameter, you can store a maximum interval value of 99 years and 11 months because the maximum interval value cannot exceed 100 years. If you want to store data with the precision that exceeds the default precision of 2 digits, you must explicitly specify this parameter with a value rather than leave this parameter empty.

    Date formats

    You can use the following formats when you insert a value of the INTERVAL YEAR TO MONTH data type. For more information about how to specify the values of interval data types, see Interval literals.

    Syntax
    Example
    Description
    INTERVAL 'year-month' YEAR(percision) TO MONTH INTERVAL '120-3' YEAR(3) TO MONTH The interval is 120 years and 3 months. Because the value of the YEAR element is greater than the default precision of 2, you must set the precision of the YEAR element to 3.
    INTERVAL 'year' YEAR(percision) INTERVAL '50' YEAR The interval is 50 years.
    INTERVAL 'month' MONTH INTERVAL '500' MONTH The interval is 500 months or 41 years and 8 months.

    Examples

    Assume that you write the following codes to create three INTERVAL YEAR TO MONTH columns named interval1 , interval2 , and interval3 in a table named Interval_Sample , and insert numeric values into the columns.

    CREATE TABLE Interval_Sample (
        interval1 INTERVAL YEAR TO MONTH,
        interval2 INTERVAL YEAR(3) TO MONTH,
        interval3 INTERVAL YEAR TO MONTH
    );
    INSERT INTO Interval_Sample (interval1, interval2, interval3)
    VALUES (INTERVAL '12-3' YEAR TO MONTH, INTERVAL '120-3' YEAR(3) TO MONTH, INTERVAL '40' MONTH);
    SELECT * FROM Interval_Sample;
    

    Return result:

    +-----------+-----------+-----------+
    | interval1 | interval2 | interval3 |
    +-----------+-----------+-----------+
    | +12-03    | +120-03   | +03-04    |
    +-----------+-----------+-----------+
    

    Calculations between interval and other date and time types

    ApsaraDB for OceanBase supports the conversion between data types. Therefore, you can perform calculations between interval values and other date values. However, the database allows you to perform addition, subtraction, multiplication, and division between data types by following rules. For more information about the matrix of calculations between date types that are currently supported, see Calculation of DATE and INTERVAL values. For more information about data type conversion, see Data type conversion.

    • Example 1 : When a calculation is performed between interval values, an interval value is returned.
    SELECT INTERVAL '2-2' YEAR TO MONTH -INTERVAL '1-1' YEAR  TO MONTH calculate1, INTERVAL '2-2' YEAR TO MONTH + INTERVAL '1-1' YEAR TO MONTH calculate2 FROM DUAL;
    

    Return result:

    +---------------+----------------+
    | calculate1    | calculate2     |
    +--------------------------------+
    | +000000001-01 | +000000003-03  |
    +---------------+----------------+
    
    • Example 2 : When a calculation is performed between interval and date and time values, a date value is returned. SYSDATE returns the current time 2020-02-27 16:13:50 . The following example returns the date value two months after the current time. The database only supports the format of an interval value plus a date and time value. The format of an interval value minus a date and time value is invalid for the calculation. However, a date time value plus an interval value and a date and time value minus an interval value are valid formats for calculations.
    SELECT TO_CHAR(INTERVAL '2' MONTH +SYSDATE,'YYYY-MM-DD HH24:MI:SS') calculate3 FROM DUAL;
    

    Return result:

    +---------------------+
    | calculate3          |
    +---------------------+
    | 2020-04-27 16:13:50 |
    +---------------------+
    
    • Example 3 : When a calculation is performed between interval and numeric values, an interval value is returned.

    Interval values can be multiplied and divided with numeric values. The following example calculates the 2-month interval multiplied by 2 and the 2-day interval divided by 3.

    SELECT INTERVAL '2' MONTH*2 calculate4, INTERVAL '2' DAY/3 calculate5 FROM DUAL;
    

    The intervals of 4 months and 16 hours are returned.

    +---------------+-------------------------------+
    | calculate4    | calculate5                    |
    +---------------+-------------------------------+
    | +000000000-04 | +000000000 16:00:00.000000000 |
    +---------------+-------------------------------+
    

    Previous topic

    TIMESTAMP WITH LOCAL TIME ZONE data type
    Last

    Next topic

    INTERVAL DAY TO SECOND data type
    Next
    What is on this page
    Syntax
    Parameter
    Date formats
    Examples
    Calculations between interval and other date and time types