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.0

    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.0
    iconOceanBase Database
    SQL - V 4.2.0
    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

    Datetime format models

    Last Updated:2023-10-31 11:17:12  Updated
    share
    What is on this page
    Datetime format elements
    Datetime format elements
    Uppercase letters in date format elements
    Punctuation and character literals in datetime format models
    Example

    folded

    share

    Datetime format models specify the format of datetime data stored in the database.

    The total length of a datetime format model cannot exceed 22 characters. If you need to convert a string value from a nondefault format to the datetime format, you can use datetime format models in functions.

    Datetime format models can be used in the following functions:

    • To convert a character value in a nondefault format to a datetime value, you need to use parameters of the functions TO_DATE, TO_TIMESTAMP and TO_TIMESTAMP_TZ to specify the format of the datetime value.

    • To convert a datetime value to a character value in a nondefault format, you need to use parameters of the TO_CHAR function to specify the format of the output string.

    If no format is specified, the default format is used for conversion. You can explicitly specify the default datetime format by using the NLS_DATE_FORMAT, NLS_TIMESTAMP_FORMAT or NLS_TIMESTAMP_TZ_FORMAT system variable. You can query the values of these variables in the V$NLS_PARAMETERS system view or modify the values of these variables by using the ALTER SESSION or ALTER SYSTEM command.

    Datetime format elements

    A datetime format model consists of one or more datetime format elements. For more information about the format elements supported by OceanBase Database, see Table of datetime format elements.

    • If a datetime format is used to parse an input string, the same format element cannot appear twice. Format elements that represent similar information cannot be combined. For example, you cannot use both SYYYY and BC elements in the same datetime format. This rule is intended to prevent conflicts caused by information redundancy.

    • Some elements cannot be used in the TO_CHAR, TO_DATE, TO_TIMESTAMP, and TO_TIMESTAMP_TZ functions.

    • Datetime format elements FF, TZD, TZH, TZM and TZR can be used in timestamp and interval format models, but not in the DATE format model.

    • Some elements generate fixed-length strings. If the length of a string is less than the specified length, blanks are added to the end of the string.

      Notice

      We recommend that you use the 4-digit year element (YYYY). Short year elements may affect query optimization because the year can only be determined at runtime.

    Datetime format elements

    Element Applicable to TO_* functions Description
    - / , . ; : "text" Yes A symbol string constant.
    AD A.D. Yes The AD indicator, with or without periods.
    AM A.M. Yes The hours before noon, with or without periods.
    BC B.C. Yes The BC indicator, with or without periods.
    CC SCC No The century.
    D Yes The day of a week. Value range: [1,7].
    DAY Yes The name of a day. The length of the result is fixed and is the same as the maximum length of the name.
    DD Yes The day of a month. Value range: [1,31].
    DDD Yes The day of a year. Value range: [1,366].
    DY Yes The abbreviated name of a day.
    FF [1..9] Yes The fractional seconds. Use digits 1 to 9 to specify the number of digits in the fractional second portion of the value returned, which is the precision. Use the default precision specified for the datetime data type. Valid in timestamp and interval formats, but not in DATE formats.
    HH Yes The hour of the day. Value range: [1,12].
    HH12 No The hour of the day. 12-hour clock. Valid values: 1 to 12.
    HH24 Yes The hour of the day, in 24-hour clock. Valid values: 0 to 23.
    IW No The week of the year, based on the ISO standard. Valid values: 1 to 52 or 53.
    IYY IY I No The last three digits, two digits, or one digit of a year based on the ISO standard.
    IYYY No A 4-digit year based on the ISO standard.
    J Yes The Julian Day.
    MI Yes The minute of an hour. Value range: [0,59].
    MM Yes The month of a year. Value range: [01,12]. 01 indicates January, and so forth.
    MON Yes The abbreviated name of a month.
    MONTH Yes The full name of a month. The length of the result is fixed and is the same as the maximum length of the name.
    PM P.M. No The hours after noon, with or without periods.
    Q No The quarter. Value range: [1,4]. Quarter 1 is from January to March, and so on.
    RR Yes The last two digits of the year. The century depends on the two digits you enter and whether the current year is in the first or second half of the century. If the current year is in the first half of the century, the result is in the current century when you enter 00 to 49, and the result is in the last century when you enter 50 to 99. For example, if the current year is 2060, the result is in the next century when you enter 00 to 49, and the result is in the current century if you enter 55 to 99.
    RRRR Yes The year. You can enter two or four digits. If you enter two digits, the rule is the same as that of the RR element.
    SS Yes The seconds. Value range: (0, 59).
    SSSSS Yes The seconds of the current day. Value range: (0, 86400).
    TZD Yes The abbreviated time zone string that contains the daylight saving time information. For example, PST indicates US/Pacific Standard Time, and PDT indicates US/Pacific Daylight Saving Time. Valid in timestamp and interval formats, but not in DATE formats.
    TZH Yes The hour part of the time zone. Valid in timestamp and interval formats, but not in DATE formats.
    TZM Yes The minute part of the time zone. Valid in timestamp and interval formats, but not in DATE formats.
    TZR Yes The region information of the time zone. Example: US/Pacific. Valid in timestamp and interval formats, but not in DATE formats.
    WW No The week of a year. Value range: [1,53].
    W No The week of a month. Value range: [1,5].
    X Yes The local delimiter used to delimit the fractional part. Example: "HH:MI:SSXFF".
    Y,YYY Yes A year with a comma (,).
    YEAR SYEAR No A year with a detailed description.
    YYYY SYYYY Yes A 4-digit year. S prefixes BC dates with a minus sign (-).
    YYY YY Y Yes The last three digits, two digits, or one digit of a year.

    Note

    The datetime functions are TO_CHAR, TO_DATE, TO_TIMESTAMP, and TO_TIMESTAMP_TZ.

    Notice

    • The conversions in the table require matching between the dates in the input string and the format elements. Otherwise, an error is returned.
    • The default date format for OceanBase Database is DD-MON-RR. To display this format, you can execute ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' to change the datetime format of the current session.

    If you omit some format elements, the system returns an error:

    obclient> SELECT TO_DATE( '31 Aug 2020', 'DD MON YYY' ) FROM DUAL;
    ORA-01830: date format picture ends before converting entire input string
    

    Uppercase letters in date format elements

    The case of date format elements affects the case of the generated string. For example, MONDAY is generated based on DAY, Monday is generated based on Day, and monday is generated based on day. Here is an example:

    obclient> SELECT TO_CHAR(sysdate,'mon')  AS nowMonth FROM DUAL;
    +----------+
    | NOWMONTH |
    +----------+
    | mar      |
    +----------+
    1 row in set
    
    obclient> SELECT TO_CHAR(sysdate,'MON')  AS nowMonth FROM DUAL;
    +----------+
    | NOWMONTH |
    +----------+
    | MAR      |
    +----------+
    1 row in set
    

    Punctuation and character literals in datetime format models

    You can add the following characters to a datetime format model, which appear in the same positions in the response:

    • Punctuation marks, such as hyphens (-), slashes (/), commas (,), periods (.), and colons (:)

    • Character literals, which are enclosed in double quotation marks (")

    OceanBase Database can flexibly convert strings into dates. When you use the TO_DATE function, if each numeric element in the input string contains the maximum allowed number of digits, the formatted string matches the input string.

    Example

    • Example 1: In the format element MM/YY, 02 corresponds to MM, and 07 corresponds to YY.

      obclient> SELECT TO_CHAR(TO_DATE('0207','MM/YY'),'MM/YY') FROM DUAL;
      +------------------------------------------+
      | TO_CHAR(TO_DATE('0207','MM/YY'),'MM/YY') |
      +------------------------------------------+
      | 02/07                                    |
      +------------------------------------------+
      1 row in set
      
    • Example 2: OceanBase Database allows matching between nonalphanumeric characters and punctuation marks in the format model, such as # for /.

      obclient> SELECT TO_CHAR (TO_DATE('02#07','MM/YY'), 'MM/YY') FROM DUAL;
      +-------------------------------------------+
      | TO_CHAR(TO_DATE('02#07','MM/YY'),'MM/YY') |
      +-------------------------------------------+
      | 02/07                                     |
      +-------------------------------------------+
      1 row in set
      

    Previous topic

    Number format models
    Last

    Next topic

    Multi-model data types
    Next
    What is on this page
    Datetime format elements
    Datetime format elements
    Uppercase letters in date format elements
    Punctuation and character literals in datetime format models
    Example