Date and time types

更新时间:
复制 MD 格式

PolarDB for Oracle supports the date and time data types described in this topic. All examples assume that the polar_comp_redwood_date configuration parameter is set to true when a table is created or altered.

Supported types

Data typeRoleStorage sizeRangePrecision
DATEDate and time, no time zone8 bytes4713 BC – 5874897 AD1 second
TIMESTAMP [(p)]Date and time, no time zone8 bytes4713 BC – 5874897 AD1 microsecond
TIMESTAMP [(p)] WITH TIME ZONEDate and time, with time zone8 bytes4713 BC – 5874897 AD1 microsecond
TIME [(p)]Time of day only8 bytes00:00:00 – 24:00:001 millisecond / 14 bits
TIME [(p)] WITH TIME ZONETime of day with time zone12 bytes00:00:00+1459 – 24:00:00-14591 millisecond / 14 bits
INTERVAL DAY TO SECOND [(p)]Duration in days, hours, minutes, and seconds12 bytes±178,000,000 years1 microsecond / 14 bits
INTERVAL YEAR TO MONTHDuration in years and months12 bytes±178,000,000 years1 microsecond / 14 bits
Note DATE, TIMESTAMP, TIME, and their WITH TIME ZONE variants each represent a point in time. INTERVAL types represent a duration between two points in time.

DATE type behavior

When DATE appears as a column data type in a data definition language (DDL) statement such as CREATE TABLE or ALTER TABLE, the value is automatically stored as TIMESTAMP(0). Time fields are therefore preserved in the column.

When DATE appears in a Stored Procedure Language (SPL) context — as a variable type in a declaration section, as a formal parameter, or as a function return type — the value is also converted to TIMESTAMP(0), so DATE can process time fields in all these contexts.

TIMESTAMP precision

TIMESTAMP [(p)] accepts an optional precision value p that specifies the number of fractional digits retained in the seconds field. Valid values are 0 through 6; the default is 6.

By default, TIMESTAMP values are stored as double-precision floating-point numbers. In this case, the effective precision may be less than 6 for dates far from January 1, 2000. If TIMESTAMP is compiled as an 8-byte integer (a compile-time option), microsecond precision is available across the full supported range, but the range is limited to 4713 BC – 294276 AD.

TIMESTAMP [(p)] WITH TIME ZONE behaves identically to TIMESTAMP [(p)] but also stores the time zone.

INTERVAL types

An INTERVAL value represents a duration. PolarDB for Oracle supports two Oracle-compatible INTERVAL subtypes.

INTERVAL DAY TO SECOND

Stores a time interval as days, hours, minutes, and seconds. The optional precision p specifies the number of fractional digits in the seconds field.

ExampleMeaning
INTERVAL '1 2:34:5.678' DAY TO SECOND(3)1 day, 2 hours, 34 minutes, 5.678 seconds
INTERVAL '1 23' DAY TO HOUR1 day, 23 hours
INTERVAL '2:34' HOUR TO MINUTE2 hours, 34 minutes
INTERVAL '2:34:56.129' HOUR TO SECOND(2)2 hours, 34 minutes, 56.13 seconds (12.9 rounded to 13 at precision 2)

INTERVAL YEAR TO MONTH

Stores a time interval as years and months.

ExampleMeaning
INTERVAL '12-3' YEAR TO MONTH12 years, 3 months
INTERVAL '45' YEAR45 years
INTERVAL '300' MONTH25 years

INTERVAL field valid values

Fields in an INTERVAL value must appear in descending order: YEAR before MONTH, DAY before HOUR, HOUR before MINUTE, MINUTE before SECOND.

FieldValid range
YEARAny integer (positive or negative)
MONTH0–11
DAYAny integer (positive or negative)
HOUR0–23
MINUTE0–59
SECOND0–59.9(p), where 9(p) is the fractional seconds precision

Date and time input

Date and time input values must be enclosed in single quotation marks using the syntax:

type 'value'

where type is DATE or TIMESTAMP, and value is a date or time string.

Use the TO_DATE function to avoid ambiguity when the input format is not guaranteed to be unambiguous.

Date input

The following formats are all equivalent to January 8, 1999. The ISO 8601 format (1999-01-08) is unambiguous regardless of locale settings and is the recommended format.

ExampleNotes
1999-01-08ISO 8601 — unambiguous in all locale settings (recommended)
January 8, 1999Unambiguous
1999-Jan-08Unambiguous
Jan-08-1999Unambiguous
08-Jan-1999Unambiguous
08-Jan-99Interpreted as January 8, 1999
Jan-08-99Interpreted as January 8, 1999
19990108ISO 8601 compact — unambiguous
990108Interpreted as January 8, 1999

If a DATE value is not appended with a time, the hour, minute, and second fields default to zero.

Time input

ExampleDescription
04:05:06.789ISO 8601
04:05:06ISO 8601
04:05ISO 8601
040506ISO 8601
04:05 AMEquivalent to 04:05 (AM has no effect)
04:05 PMEquivalent to 16:05 (hour must be ≤ 12)
04:05:06.789-8ISO 8601 with UTC offset
04:05:06-08:00ISO 8601 with UTC offset
04:05-08:00ISO 8601 with UTC offset
040506-08ISO 8601 with UTC offset
04:05:06 PSTTime zone abbreviation
2003-04-12 04:05:06America/New_YorkTime zone by name

Timestamp input

A timestamp combines a date and a time. The date and time fields follow the formats listed above.

FormatExample
Oracle default (dd-MON-yy)08-JAN-99 04:05:06
ISO 86011999-01-08 04:05:06

Date and time output

The output format depends on the client:

ClientFormatExample
psqlRedwood style (dd-MON-yy)31-DEC-05 07:37:16
Java Database Connectivity (JDBC)ISO 8601 (yyyy-mm-dd)1997-12-17 07:37:16

Internal formats

PolarDB calculates all date and time values using Julian dates, assuming 365.2425 days per year. This allows correct date calculations for dates as far back as 4713 BC.