Skip to content

Add CAST to DECIMAL with parameters to OQL docs #9986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,16 @@ The table below describes which `CAST` conversions are supported:

Converting `DATETIME` or `BOOLEAN` to `STRING` returns different format per database.

##### `DECIMAL` precision

`DECIMAL` data type can have precision and scale as parameters:

- `DECIMAL(<precision>, <scale>)` — in the case when both parameters are specified, those values are used as precision and scale of the resulting data type
- `DECIMAL(<precision>)` — when only precision is specified, scaale is set to 0. The resulting data type in that case is `DECIMAL(<precision>, 0)`
- `DECIMAL` — when no parameters are specified, default values are used. Precision is set to 28, and scale is set to 8: `DECIMAL(28, 8)`

If the original value has more digits in the fractional part than required scale, the fractional part is rounded to the required scale. In that case, rounding is done according to the database configuration.

#### Examples

A frequent use case for `CAST` is to convert your date from the `DATETIME` data type to a text formatted `STRING` type:
Expand All @@ -758,6 +768,21 @@ SELECT (Number : 2) as Normal, (Cast(Number AS DECIMAL) : 2) as Casted FROM Sale
| 1 | 1.0 |
| 1 | 1.5 |

In case of conversion to `DECIMAL`, scale and precision can be specified

```sql
SELECT
CAST('123.0987654321' AS DECIMAL) AS default_decimal,
CAST('123.0987654321' AS DECIMAL(20)) AS decimal_precision,
CAST('123.0987654321' AS DECIMAL(20, 6)) AS decimal_precision_scale
FROM Sales.Order
LIMIT 1
```

| default_decimal | decimal_precision | decimal_precision_scale |
|------:|-------:|-------:|
| 123.09876543 | 123 | 123.098765 |

### COALESCE {#coalesce-expression}

Returns the value of the first `expression` that is not NULL. Can be used with columns.
Expand Down