PolarDB for PostgreSQL (Compatible with Oracle) supports the following date and time data types.
polar_comp_redwood_date configuration parameter is set to true when a table is created or altered.Supported types
| Type | Storage size | Description | Min value | Max value | Resolution |
|---|---|---|---|---|---|
| DATE | 8 bytes | Date and time, without time zone | 4713 BC | 5874897 AD | 1 second |
| INTERVAL DAY TO SECOND [(p)] | 12 bytes | Time interval in days, hours, minutes, and seconds | -178000000 years | 178000000 years | 1 microsecond / 14 bits |
| INTERVAL YEAR TO MONTH | 12 bytes | Time interval in years and months | -178000000 years | 178000000 years | 1 microsecond / 14 bits |
| TIMESTAMP [(p)] | 8 bytes | Date and time, without time zone | 4713 BC | 5874897 AD | 1 microsecond |
| TIMESTAMP [(p)] WITH TIME ZONE | 8 bytes | Date and time, with time zone | 4713 BC | 5874897 AD | 1 microsecond |
| TIME [(p)] | 8 bytes | Time of day only | 00:00:00 | 24:00:00 | 1 millisecond / 14 bits |
| TIME [(p)] WITH TIME ZONE | 12 bytes | Time of day with time zone | 00:00:00+1459 | 24:00:00-1459 | 1 millisecond / 14 bits |
DATE type
When DATE appears as a column type in data definition language (DDL) statements such as CREATE TABLE or ALTER TABLE, PolarDB converts the DATE value to TIMESTAMP(0) when storing the table definition. Time fields are also stored.
When DATE appears in Stored Procedure Language (SPL) — as a variable type in a declaration section, as a formal parameter type in an SPL procedure or function, or as the return type of an SPL function — PolarDB also converts the DATE value to TIMESTAMP(0), enabling DATE to store time fields.
TIMESTAMP type
TIMESTAMP accepts an optional precision value p that specifies the number of fractional digits retained in the seconds field. Valid values of p range from 0 to 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. Values are stored as seconds before or after midnight on January 1, 2000. Dates within a few years of January 1, 2000 achieve microsecond precision, but precision degrades for dates further away.
When stored as an 8-byte integer (a compile-time option), microsecond precision is available for the full range of values. However, the supported date range is limited to 4713 BC to 294276 AD.
TIMESTAMP [(p)] WITH TIME ZONE behaves identically to TIMESTAMP [(p)], but also stores the time zone.
INTERVAL types
INTERVAL values specify a period of time and are composed of the following fields:
| Field | Valid values |
|---|---|
| YEAR | Any integer (positive or negative) |
| MONTH | 0 to 11 |
| DAY | Any integer (positive or negative) |
| HOUR | 0 to 23 |
| MINUTE | 0 to 59 |
| SECOND | 0 to 59.9(p), where 9(p) is the fractional seconds precision |
Fields must appear in descending order: YEAR before MONTH, DAY before HOUR, and HOUR before MINUTE before SECOND.
PolarDB supports two Oracle-compatible INTERVAL types.
INTERVAL DAY TO SECOND [(p)]
Stores a time interval in days, hours, minutes, and seconds. p specifies the precision of the seconds field.
Examples:
| Literal | Value |
|---|---|
INTERVAL '1 2:34:5.678' DAY TO SECOND(3) | 1 day, 2 hours, 34 minutes, 5 seconds, and 678 thousandths of a second |
INTERVAL '1 23' DAY TO HOUR | 1 day and 23 hours |
INTERVAL '2:34' HOUR TO MINUTE | 2 hours and 34 minutes |
INTERVAL '2:34:56.129' HOUR TO SECOND(2) | 2 hours, 34 minutes, 56 seconds, and 13 thousandths of a second |
0.129 is rounded up to 0.13 based on the specified precision of 2.INTERVAL YEAR TO MONTH
Stores a time interval in years and months.
Examples:
| Literal | Value |
|---|---|
INTERVAL '12-3' YEAR TO MONTH | 12 years and 3 months |
INTERVAL '45' YEAR | 45 years |
INTERVAL '300' MONTH | 25 years |
Date and time input
Date and time input is accepted in ISO 8601 format, the Oracle default dd-MON-yy format, and other unambiguous formats. To avoid ambiguity, use the TO_DATE function.
Enclose each date or time value in single quotation marks, using the following syntax:
type 'value' typeWhere type is DATE or TIMESTAMP, and value is a date or time string.
Date input
The following formats are all equivalent to January 8, 1999:
January 8, 19991999-01-081999-Jan-08Jan-08-199908-Jan-199908-Jan-99Jan-08-9919990108990108
If a DATE value is not followed by a time value, the hour, minute, and second fields are set to zero.
Time input
| Example | Description |
|---|---|
04:05:06.789 | ISO 8601 |
04:05:06 | ISO 8601 |
04:05 | ISO 8601 |
040506 | ISO 8601 |
04:05 AM | Equivalent to 04:05. AM does not affect the value. |
04:05 PM | Equivalent to 16:05. The hour value must be 12 or less. |
04:05:06.789-8 | ISO 8601 |
04:05:06-08:00 | ISO 8601 |
04:05-08:00 | ISO 8601 |
040506-08 | ISO 8601 |
04:05:06 PST | Time zone abbreviation |
2003-04-12 04:05:06America/New_York | Time zone by name |
Timestamp input
A valid timestamp consists of a date followed by a time. The date and time components accept the formats described above.
Oracle format:
08-JAN-99 04:05:06ISO 8601 format:
1999-01-08 04:05:06Date and time output
The output format depends on the client interface:
Java Database Connectivity (JDBC): displays dates in ISO 8601 format (
yyyy-mm-dd)psql: displays dates in Redwood format (
dd-MON-yy), which is Oracle-compatible
| Format | Example |
|---|---|
| Redwood style | 31-DEC-05 07:37:16 |
| ISO 8601/SQL standard | 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 ensures accurate date calculations for dates after 4713 BC.