You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: data-types.md
+13-5Lines changed: 13 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,12 +26,20 @@ Type | Description | Example
26
26
27
27
CockroachDB supports explicit type conversions using the following methods:
28
28
29
-
-`<value>::<data type>`, or its equivalent longer form `CAST(<value> AS <data type>)`, which converts an arbitrary expression of one built-in type to another (this is also known as "casting"). For example:
30
-
`NOW()::DECIMAL`, `VARIANCE(a+2)::INT`.
31
-
32
29
-`<type> 'string literal'`, to convert from the literal representation of a value to a value of that type. For example:
33
-
`DATE '2008-12-21'`, `INT '123'`, or `BOOL 'true'`.
30
+
`DATE '2008-12-21'`, `INT '123'`, or `BOOL 'true'`.
31
+
32
+
-`<value>::<data type>`, or its equivalent longer form `CAST(<value> AS <data type>)`, which converts an arbitrary expression of one built-in type to another (this is also known as type coercion or "casting"). For example:
instead of a cast, as it provides more predictable results.
39
+
{{site.data.alerts.end}}
34
40
35
41
- Other [built-in conversion functions](functions-and-operators.html) when the type is not a SQL type, for example `from_ip()`, `to_ip()` to convert IP addresses between `STRING` and `BYTES` values.
36
42
37
-
You can find each data type's supported converstion and casting on its respective page in the **Supported Casting & Conversion** section.
43
+
44
+
You can find each data type's supported converstion and casting on its
45
+
respective page in its section **Supported Casting & Conversion**.
Copy file name to clipboardExpand all lines: decimal.md
+11-14Lines changed: 11 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,11 +26,10 @@ When inserting a decimal value:
26
26
- If digits to the left and right of the decimal point exceed the column's `precision`, CockroachDB gives an error.
27
27
- If the column's `precision` and `scale` are identical, the inserted value must round to less than 1.
28
28
29
-
## Format
29
+
## Syntax
30
30
31
-
When inserting into a `DECIMAL` column, format the value as a numeric literal, e.g., `1.2345` or `1`.
32
-
33
-
Alternately, you can cast a float as a decimal: `CAST(1.2345 AS DECIMAL)`. However, note that the precision will be limited to 17 digits in total (both to the left and right of the decimal point).
31
+
A constant value of type `DECIMAL` can be entered as a [numeric literal](sql-constants.html#numeric-literals).
32
+
For example: `1.414` or `-1234`.
34
33
35
34
## Size
36
35
@@ -53,19 +52,19 @@ The size of a `DECIMAL` value is variable, starting at 9 bytes. It's recommended
53
52
+-------+---------------+-------+---------+
54
53
~~~
55
54
~~~sql
56
-
>INSERT INTO decimals VALUES (1.01234567890123456789, 1.01234567890123456789, CAST(1.01234567890123456789ASDECIMAL));
55
+
>INSERT INTO decimals VALUES (1.01234567890123456789, 1.01234567890123456789, 1.01234567890123456789);
# The value in "a" matches what was inserted exactly.
67
66
# The value in "b" has been rounded to the column's scale.
68
-
# The value in "c" has been limited to 17 digits.
67
+
# The value in "c" is handled like "a" because NUMERIC is an alias.
69
68
~~~
70
69
71
70
## Supported Casting & Conversion
@@ -79,8 +78,6 @@ Type | Details
79
78
`BOOL` | **0** converts to `false`; all other values convert to `true`
80
79
`STRING` | ––
81
80
82
-
{{site.data.alerts.callout_info}}Because the <ahref="serial.html"><code>SERIAL</code> data type</a> represents values automatically generated CockroachDB to uniquely identify rows, you cannot meaningfully cast other data types as <code>SERIAL</code> values.{{site.data.alerts.end}}
Copy file name to clipboardExpand all lines: float.md
+19-11Lines changed: 19 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,10 @@ summary: The FLOAT data type stores inexact, floating-point numbers with up to 1
4
4
toc: false
5
5
---
6
6
7
-
The `FLOAT`[data type](data-types.html) stores inexact, floating-point numbers with up to 17 digits in total and at least one digit to the right of the decimal point.
7
+
The `FLOAT`[data type](data-types.html) stores inexact, floating-point numbers with up to 17 digits of decimal precision.
8
+
9
+
They are handled internally using the [standard double-precision
@@ -15,14 +18,20 @@ In CockroachDB, the following are aliases for `FLOAT`:
15
18
-`REAL`
16
19
-`DOUBLE PRECISION`
17
20
18
-
## Format
21
+
## Syntax
19
22
20
-
When inserting into a `FLOAT` column, format the value as a numeric literal, e.g., `1.2345` or `1`.
23
+
A constant value of type `FLOAT` can be entered as a [numeric literal](sql-constants.html#numeric-literals).
24
+
For example: `1.414` or `-1234`.
21
25
22
-
Alternately, you can cast `+Inf` (positive infinity), `-Inf` (negative infinity), or `NaN` (not a number) as a float:
26
+
The special IEEE754 values for positive infinity, negative infinity
27
+
and Not A Number (NaN) cannot be entered using numeric literals
28
+
directly and must be converted using an
29
+
[interpreted literal](sql-constants.html#interpreted-literals) or an
30
+
[explicit conversion](sql-expressions.html#explicit-type-coercions) from
31
+
a string literal instead. For example:
23
32
24
-
-`CAST('+Inf' AS FLOAT)`
25
-
-`CAST('-Inf' AS FLOAT)`
33
+
-`FLOAT '+Inf'`
34
+
-`'-Inf'::FLOAT`
26
35
-`CAST('NaN' AS FLOAT)`
27
36
28
37
## Size
@@ -61,16 +70,15 @@ A `FLOAT` column supports values up to 8 bytes in width, but the total storage s
61
70
62
71
## Supported Casting & Conversion
63
72
64
-
`DECIMAL` values can be [cast](data-types.html#data-type-conversions--casts) to any of the following data types:
73
+
`FLOAT` values can be [cast](data-types.html#data-type-conversions--casts) to any of the following data types:
65
74
66
75
Type | Details
67
76
-----|--------
68
77
`INT` | Truncates decimal precision and requires values to be between -2^63 and 2^63-1
69
-
`DECIMAL` | ––
78
+
`DECIMAL` | Causes an error to be reported if the value is NaN or +/- Inf.
70
79
`BOOL` | **0** converts to `false`; all other values convert to `true`
71
-
72
-
{{site.data.alerts.callout_info}}Because the <ahref="serial.html"><code>SERIAL</code> data type</a> represents values automatically generated CockroachDB to uniquely identify rows, you cannot meaningfully cast other data types as <code>SERIAL</code> values.{{site.data.alerts.end}}
Copy file name to clipboardExpand all lines: int.md
+5-14Lines changed: 5 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,17 +20,10 @@ In CockroachDB, the following are aliases for `INT`:
20
20
-`INT64`
21
21
-`BIGINT`
22
22
23
-
## Formats
23
+
## Syntax
24
24
25
-
An `INT` column accepts numeric literals and hexadecimal-encoded numeric literals.
26
-
27
-
### Numeric Literal
28
-
29
-
When inserting a numeric literal into an `INT` column, format the value as `12345`.
30
-
31
-
### Hexadecimal-Encoded Numeric Literal
32
-
33
-
When inserting a hexadecimal-encoded numeric literal into a `INT` column, format the value as hexadecimal digits preceded by `0x`. For example, `0xcafe1111` corresponds to the numeric literal `3405648145`.
25
+
A constant value of type `INT` can be entered as a [numeric literal](sql-constants.html#numeric-literals).
26
+
For example: `42`, `-1234` or `0xCAFE`.
34
27
35
28
## Size
36
29
@@ -74,15 +67,13 @@ CockroachDB does not offer multiple integer types for different widths; instead,
74
67
Type | Details
75
68
-----|--------
76
69
`DECIMAL` | ––
77
-
`FLOAT` | Requires `INT` value to be less than 2^53
70
+
`FLOAT` | Loses precision if the `INT` value is larger than 2^53 in magnitude
78
71
`BOOL` | **0** converts to `false`; all other values convert to `true`
79
72
`DATE` | Converts to days since the Unix epoch (Jan. 1, 1970)
80
73
`TIMESTAMP` | Converts to seconds since the Unix epoch (Jan. 1, 1970)
81
74
`INTERVAL` | Converts to microseconds
82
75
`STRING` | ––
83
76
84
-
{{site.data.alerts.callout_info}}Because the <ahref="serial.html"><code>SERIAL</code> data type</a> represents values automatically generated CockroachDB to uniquely identify rows, you cannot meaningfully cast other data types as <code>SERIAL</code> values.{{site.data.alerts.end}}
[coerced to](sql-expressions.html#explicit-type-coercions) type
19
+
`INTERVAL`.
20
+
21
+
`INTERVAL` constants can be expressed using the following formats:
14
22
15
23
Format | Description
16
24
-------|--------
17
-
Golang | `INTERVAL '1h2m3s4ms5us6ns'`<br><br>Note that `ms` is milliseconds, `us` is microseconds, and `ns` is nanoseconds. Also, all fields support both integers and floats.
18
-
Traditional Postgres | `INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'`
19
-
ISO 8601 | `INTERVAL 'P1Y2M3DT4H5M6S'`
20
25
SQL Standard | `INTERVAL 'Y-M D H:M:S'`<br><br>`Y-M D`: Using a single value defines days only; using two values defines years and months. Values must be integers.<br><br>`H:M:S`: Using a single value defines seconds only; using two values defines hours and minutes. Values can be integers or floats.<br><br>Note that each side is optional.
26
+
ISO 8601 | `INTERVAL 'P1Y2M3DT4H5M6S'`
27
+
Traditional PostgreSQL | `INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'`
28
+
Golang | `INTERVAL '1h2m3s4ms5us6ns'`<br><br>Note that `ms` is milliseconds, `us` is microseconds, and `ns` is nanoseconds. Also, all fields support both integers and floats.
21
29
22
-
Alternatively, you can use a string literal, e.g., `'1h2m3s4ms5us6ns'` or`'1 year 2 months 3 days 4 hours 5 minutes 6 seconds'`, which CockroachDB will resolve into the `INTERVAL` type.
30
+
CockroachDB also supports using uninterpreted
31
+
[string literals](sql-constants.html#string-literals) in contexts
32
+
where a `INTERVAL` value is otherwise expected.
23
33
24
34
Intervals are stored internally as months, days, and nanoseconds.
25
35
@@ -51,9 +61,9 @@ CREATE TABLE
51
61
~~~
52
62
53
63
~~~sql
54
-
>INSERT INTO intervals VALUES
55
-
(1, INTERVAL '1h2m3s4ms5us6ns'),
56
-
(2, INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'),
64
+
>INSERT INTO intervals VALUES
65
+
(1, INTERVAL '1h2m3s4ms5us6ns'),
66
+
(2, INTERVAL '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'),
0 commit comments