OceanBase logo

OceanBase

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

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

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

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 - V4.2.1

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & Certification
    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. V4.2.1
    iconOceanBase Database
    SQL - V 4.2.1
    SQL
    KV
    • 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

    Calculation of dates, timestamps, and intervals

    Last Updated:2023-12-25 08:49:42  Updated
    share
    What is on this page
    share

    You can perform arithmetic operations on the following data types: DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE, INTERVAL YEAR TO MONTH, and INTERVAL DAY TO SECOND. This topic describes the calculation of dates, timestamps, and intervals.

    OceanBase Database performs the calculation based on the following rules:

    • You can use the NUMBER constant in arithmetic operations for date and timestamp values instead of interval values. OceanBase Database converts a timestamp value to a date value and explains the NUMBER constant in arithmetic datetime and interval expressions as days. For example, SYSDATE +1 specifies tomorrow. SYSDATE-7 specifies one week ago. SYSDATE +(10/1440) specifies ten minutes later.

      Notice

      The date and timestamp values cannot be used in multiplication and division.

    • OceanBase Database converts the BINARY_FLOAT and BINARY_DOUBLE operands to NUMBER.

    • Each DATE value includes a time component, and many date operation results include a fraction. This fraction indicates a fraction of a day. For example, if the value of DATE is 1.5, it indicates 36 hours. The built-in functions of OceanBase Database return these fractions to perform common operations on the DATE data. For example, the MONTHS_BETWEEN function returns the number of months between two dates. The fractional part of the result indicates a fraction of a 31-day month.

    • When an operand is a DATE or numeric value that does not include a time zone or fractional second, the calculation is performed in three cases:

      • Generally, OceanBase Database converts other operands to DATE values, with an exception that the operation where a number multiplies an interval always returns an interval.
      • If the other operand includes a time zone value, OceanBase Database uses the time zone of the current session in the returned value.
      • If the other operand includes a fractional second value, the fractional second value is discarded.
    • When a timestamp, interval, or numeric value is passed to a built-in function that applies only to the DATE data type, OceanBase Database converts non-DATE values to DATE values.

    • When an interval calculation returns a datetime value, the returned result must be the actual datetime value. Otherwise, an error is returned. Here are some examples:

      SELECT TO_DATE("31-AUG-2004','DD-MON-YYYY") + TO_YMINTERVAL("0-1") FROM DUAL;
      SELECT TO_DATE("29-FEB-2004','DD-MON-YYYY") + TO_YMINTERVAL("1-0") FROM DUAL;
      

      In the first statement, a month is added to a 31-day month. The calculation result is September 31, which is an invalid date. Therefore, the statement execution failed. In the second statement, a year is added on February 29, which appears every four years. The calculation result is invalid. Therefore, the statement execution failed. If four years are added to February 29, the calculation result is February 29, 2008, which is valid.

      SELECT TO_DATE("29-FEB-2004", "DD-MON-YYYY") + TO_YMINTERVAL("4-0") FROM DUAL;
      

    Previous topic

    INTERVAL DAY TO SECOND data type
    Last

    Next topic

    RAW data type
    Next