Data types and implicit type conversion
CoraDB provides the following groups of data types. Each group is described on its own page.
| Group | Types | Page |
|---|---|---|
| Numeric | SHORT/SMALLINT, INTEGER, BIGINT, NUMERIC/DECIMAL, FLOAT/REAL, DOUBLE | Numeric types |
| Date/time | DATE, TIME, TIMESTAMP, DATETIME, and the types WITH TIME ZONE / WITH LOCAL TIME ZONE | Date/time types |
| Strings | CHAR, VARCHAR/STRING, BIT, BIT VARYING, ENUM | String types |
| Large objects | BLOB, CLOB | BLOB/CLOB types |
| Collections | SET, MULTISET, LIST/SEQUENCE | Collection types |
| JSON | JSON | JSON data type |
| Graph | GRAPH_OBJECT, PATH | Graph types |
To convert a value explicitly, use CAST or one of the other type conversion functions. When an operation combines values of different types without a cast, CoraDB converts them as described below.
Implicit type conversion
Section titled “Implicit type conversion”An implicit type conversion is a conversion CoraDB applies on its own when an expression has a different type from the one the operation expects: a value assigned to a column, a function argument, or the operands of a comparison or arithmetic operator.
General rules:
- Collection types (SET, MULTISET, LIST/SEQUENCE) are never converted implicitly. Use CAST.
- Converting DATETIME or TIMESTAMP (with or without time zone) to DATE or TIME drops part of the value. Converting DATE to DATETIME or TIMESTAMP sets the time to midnight (12:00:00 AM).
- Values of the types with a time zone are stored as UTC. Converting them to a type without time zone, or the other way round, uses the session time zone. See Date/time types.
- Converting a string or an exact numeric value to FLOAT or DOUBLE can lose precision: strings and exact types are decimal, floating-point types are binary.
Conversion tables
Section titled “Conversion tables”✓ means that CoraDB converts the type in the row to the type in the column implicitly. An empty cell means that the conversion needs an explicit CAST, or is not possible.
Date/time targets
| From \ to | DATETIME | DATETIMELTZ | DATETIMETZ | DATE | TIME | TIMESTAMP | TIMESTAMPLTZ | TIMESTAMPTZ |
|---|---|---|---|---|---|---|---|---|
| DATETIME | — | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| DATETIMELTZ | ✓ | — | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| DATETIMETZ | ✓ | ✓ | — | ✓ | ✓ | ✓ | ✓ | ✓ |
| DATE | ✓ | ✓ | ✓ | — | ✓ | ✓ | ✓ | |
| TIME | — | |||||||
| TIMESTAMP | ✓ | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ |
| TIMESTAMPLTZ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — | ✓ |
| TIMESTAMPTZ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — |
| DOUBLE | ✓ | ✓ | ✓ | ✓ | ||||
| FLOAT | ✓ | ✓ | ✓ | ✓ | ||||
| NUMERIC | ✓ | ✓ | ✓ | |||||
| BIGINT | ✓ | ✓ | ✓ | ✓ | ||||
| INT | ✓ | ✓ | ✓ | ✓ | ||||
| SHORT | ✓ | ✓ | ✓ | ✓ | ||||
| BIT | ||||||||
| VARBIT | ||||||||
| CHAR | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| VARCHAR | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Numeric values converted to TIME or TIMESTAMP
- Integer and floating-point values can be converted to TIME. The number is taken as seconds, modulo 86,400 (one day):
CAST(90000 AS TIME)is01:00:00 AM. NUMERIC values cannot be converted to TIME. - All numeric types, including NUMERIC, can be converted to TIMESTAMP, TIMESTAMPLTZ and TIMESTAMPTZ. The number is taken as seconds since 1970-01-01 00:00:00 UTC and cannot exceed 2,147,483,647.
Numeric, bit string and string targets
| From \ to | INT | SHORT | BIT | VARBIT | CHAR | VARCHAR | DOUBLE | FLOAT | NUMERIC | BIGINT |
|---|---|---|---|---|---|---|---|---|---|---|
| DATETIME | ✓ | ✓ | ||||||||
| DATETIMELTZ | ✓ | ✓ | ||||||||
| DATETIMETZ | ✓ | ✓ | ||||||||
| DATE | ✓ | ✓ | ||||||||
| TIME | ✓ | ✓ | ||||||||
| TIMESTAMP | ✓ | ✓ | ||||||||
| TIMESTAMPLTZ | ✓ | ✓ | ||||||||
| TIMESTAMPTZ | ✓ | ✓ | ||||||||
| DOUBLE | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | ✓ | ||
| FLOAT | ✓ | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | ||
| NUMERIC | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — | ✓ | ||
| BIGINT | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | — | ||
| INT | — | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
| SHORT | ✓ | — | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ||
| BIT | — | ✓ | ✓ | ✓ | ||||||
| VARBIT | ✓ | — | ✓ | ✓ | ||||||
| CHAR | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | ✓ | ✓ | ✓ |
| VARCHAR | ✓ | ✓ | ✓ | ✓ | ✓ | — | ✓ | ✓ | ✓ | ✓ |
Conversion rules
Section titled “Conversion rules”INSERT and UPDATE
Section titled “INSERT and UPDATE”A value is converted to the type of the column it is assigned to, and rounded or rejected according to that type. Here a string is assigned to the SHORT column units_in_stock and to the NUMERIC(10,2) column unit_price:
UPDATE product SET units_in_stock = '25', unit_price = '31.499' WHERE id = 1618;SELECT id, unit_price, units_in_stock FROM product WHERE id = 1618;| id | unit_price | units_in_stock |
|---|---|---|
| 1618 | 31.50 | 25 |
Functions
Section titled “Functions”When a function expects a given type and the argument can be converted to it, the argument is converted. MOD expects numbers, so the two strings are converted to DOUBLE:
SELECT MOD('123', '2') AS m, TYPEOF(MOD('123', '2')) AS t;| m | t |
|---|---|
| 1 | double |
When a function accepts several types and receives a mix, all arguments are converted to the type with the highest priority in its group:
- Date/time: DATETIME > TIMESTAMP > DATE > TIME
- Approximate numeric: DOUBLE > FLOAT
- Exact numeric: NUMERIC > BIGINT > INT > SHORT
- String: CHAR > VARCHAR
Comparison operations
Section titled “Comparison operations”The operands of a comparison are converted as follows.
| Operand 1 | Operand 2 | Conversion | Compared as |
|---|---|---|---|
| Numeric | Numeric | none | Number |
| Numeric | String | Operand 2 to DOUBLE | Number |
| Numeric | Date/time | Operand 1 to date/time | TIME/TIMESTAMP |
| String | Numeric | Operand 1 to DOUBLE | Number |
| String | String | none | string |
| String | Date/time | Operand 1 to date/time | date/time |
| Date/time | Numeric | Operand 2 to date/time | TIME/TIMESTAMP |
| Date/time | String | Operand 2 to date/time | date/time |
| Date/time | Date/time | To the type with the higher priority | date/time |
For date/time and numeric operands, the limits for numeric values converted to TIME or TIMESTAMP apply.
Exception: string column against a numeric or date/time value. When operand 1 is a string column (or other non-constant expression) and operand 2 is a numeric or date/time value (a literal or a host variable), operand 2 is converted to a string and the comparison is a string comparison. The exception does not apply to the set operators (IN, NOT IN, = ALL, = ANY, < ALL, < ANY, <= ALL, <= ANY, >= ALL, >= ANY).
Between a numeric value and a string literal, the string is converted to DOUBLE:
SELECT ('2' <= 11) AS a, ('5' > 11) AS b, ('2010-01-01' < DATE'2010-02-02') AS c;| a | b | c |
|---|---|---|
| 1 | 0 | 1 |
An INT column compared with a string literal is compared as a number; 1,593 products have an ID below 1603.5:
SELECT COUNT(*) AS n FROM product WHERE id < '1603.5';| n |
|---|
| 1593 |
city.postal_code is a STRING column, so comparing it with numbers is a string comparison. '10100' and '10785' sort between '1000' and '1100', while a numeric range would not include them:
SELECT name, postal_code FROM city WHERE postal_code BETWEEN 1000 AND 1100 ORDER BY postal_code;| name | postal_code |
|---|---|
| Buenos Aires | 1010 |
| Torino | 10100 |
| Berlin | 10785 |
| Caracas | 1081 |
The same happens with a numeric host variable, for example WHERE postal_code BETWEEN ? AND ? bound to 1000 and 1100. Compare a string column with a quoted string, or convert it explicitly, to get the comparison you intend.
Comparing a DATE column with a string literal converts the string to a date, which is the usual way to filter on dates:
SELECT COUNT(*) AS n FROM orders WHERE order_date >= '2025-12-01';| n |
|---|
| 95 |
Range and set comparisons
Section titled “Range and set comparisons”With the set operators the exception above does not apply, so a numeric column compared with a set of strings is compared numerically:
SELECT id FROM orders WHERE id <= ALL {'3', '5'} ORDER BY id;| id |
|---|
| 0 |
| 1 |
| 2 |
| 3 |
A string operand that cannot be converted to the other type raises an error.
Arithmetic operations
Section titled “Arithmetic operations”Date/time operands. When the operands of - are two different date/time types, the one with the lower priority is converted. Here the DATE is converted to DATETIME, so the result is in milliseconds:
SELECT DATE'2002-01-01' - DATETIME'2001-02-02 12:00:00 AM' AS ms;| ms |
|---|
| 28771200000 |
Numeric operands. Operands of different numeric types are converted to the type with the higher priority. See Coercion in expressions.
Date/time and numeric operands. With + or -, the numeric operand is converted to BIGINT, INT or SHORT and counts units of the date/time type: days for DATE, seconds for TIME and TIMESTAMP, milliseconds for DATETIME.
Date/time and string operands. Only + and - are allowed.
- With
+, the string is converted to a BIGINT number of units (as above, fractions rounded) and the result has the date/time type. - With
-, the string is converted to DATETIME (or to TIME when the other operand is a TIME) and the result is a BIGINT difference in milliseconds (seconds for TIME).
SELECT DATE'2002-01-01' + '10' AS plus_days, DATE'2002-01-01' - '2001-01-01' AS diff_ms;| plus_days | diff_ms |
|---|---|
| 2002-01-11 | 31536000000 |
A string that is not a date/time cannot be subtracted:
SELECT DATE'2002-01-01' - '10';ERROR: Conversion error in timestamp format.Numeric and string operands. The string is converted to DOUBLE and the result is DOUBLE:
SELECT 4 + '5.2' AS a, TYPEOF(4 + '5.2') AS t;| a | t |
|---|---|
| 9.2 | double |
A string that looks like a date is not converted to a date in this case. Use a date literal instead:
SELECT '2002-01-01' + 1;ERROR: Cannot coerce '2002-01-01' to type double.SELECT DATE'2002-01-01' + 1 AS next_day;| next_day |
|---|
| 2002-01-02 |
String operands. *, / and - between two strings convert both to numbers and return a DOUBLE:
SELECT '3' * '2' AS a, TYPEOF('3' * '2') AS t;| a | t |
|---|---|
| 6 | double |
+ between two strings depends on the system parameter plus_as_concat:
yes(default): the strings are concatenated, so'1' + '1'is'11'.no: if both strings can be converted to numbers they are added as DOUBLE, so'1' + '1'is2.
SELECT '1' + '1' AS r;| r |
|---|
| 11 |