|
| 1 | +--- |
| 2 | +title: TO_DECIMAL |
| 3 | +--- |
| 4 | + |
| 5 | +Converts a value to DECIMAL data type. |
| 6 | + |
| 7 | +## Syntax |
| 8 | + |
| 9 | +```sql |
| 10 | +TO_DECIMAL |
| 11 | +( <expr> [, '<format>' ] [, <precision> [, <scale> ] ] ) |
| 12 | +``` |
| 13 | + |
| 14 | +## Arguments |
| 15 | + |
| 16 | +| Arguments | Description | |
| 17 | +|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 18 | +| expr | An expression of a boolean, number, string, or variant type. | |
| 19 | +| format | Optional. A format string specifying the desired precision and scale for the decimal result. It may define the total number of digits and the number of digits after the decimal point. | |
| 20 | +| precision | Optional. Total number of significant digits in the result, the default is 38. | |
| 21 | +| scale | Optional. Number of digits after the decimal point. Must not exceed precision, the default is 0. | |
| 22 | + |
| 23 | +### Format Specifiers |
| 24 | + |
| 25 | +| Format Specifier | Description | Example Input | |
| 26 | +|--------------------|---------------------------------------------------------------------------------|---------------| |
| 27 | +| '9' | Optional digit (accepts digit or nothing, but this implementation requires one) | "123" | |
| 28 | +| '0' | Required digit (same as 9 in current implementation) | "123" | |
| 29 | +| 'D' | Decimal point (accepts '.' or ',', always normalized to '.') | "12.34" | |
| 30 | +| 'G' | Group (thousands) separator (expects ',' in input) | "1,234" | |
| 31 | +| 'S' | Sign ('+' or '-', applied as prefix to result) | "-123" | |
| 32 | +| Literal character | Any literal character must appear in the input exactly | "NT$123" | |
| 33 | + |
| 34 | +## Examples |
| 35 | + |
| 36 | +```sql |
| 37 | +SELECT TO_DECIMAL('1234.56'); |
| 38 | + |
| 39 | +╭───────────────────────╮ |
| 40 | +│ to_decimal('1234.56') │ |
| 41 | +├───────────────────────┤ |
| 42 | +│ 1235 │ |
| 43 | +╰───────────────────────╯ |
| 44 | + |
| 45 | +SELECT TO_DECIMAL('1234.56', '9999D99'); |
| 46 | + |
| 47 | +╭──────────────────────────────────╮ |
| 48 | +│ to_decimal('1234.56', '9999D99') │ |
| 49 | +├──────────────────────────────────┤ |
| 50 | +│ 1235 │ |
| 51 | +╰──────────────────────────────────╯ |
| 52 | + |
| 53 | +SELECT TO_DECIMAL('1234.56', 38, 1); |
| 54 | + |
| 55 | +╭──────────────────────────────╮ |
| 56 | +│ to_decimal('1234.56', 38, 1) │ |
| 57 | +├──────────────────────────────┤ |
| 58 | +│ 1234.6 │ |
| 59 | +╰──────────────────────────────╯ |
| 60 | + |
| 61 | +SELECT TO_DECIMAL('NT$1234.56', 'NT$9999D99', 38, 1); |
| 62 | + |
| 63 | +╭───────────────────────────────────────────────╮ |
| 64 | +│ to_decimal('NT$1234.56', 'NT$9999D99', 38, 1) │ |
| 65 | +├───────────────────────────────────────────────┤ |
| 66 | +│ 1234.6 │ |
| 67 | +╰───────────────────────────────────────────────╯ |
| 68 | + |
| 69 | +``` |
0 commit comments