16.20 - Result Type - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Functions, Expressions, and Predicates

Product
Advanced SQL Engine
Teradata Database
Release Number
16.20
Published
March 2019
Language
English (United States)
Last Update
2020-03-25
dita:mapPath
xzf1512079057909.ditamap
dita:ditavalPath
TD_DBS_16_20_Update1.ditaval
dita:id
kby1472250656485

DECODE is a scalar function whose return value data type depends on the data type associated with the result parameter passed into the function.

  • If result or default is a numeric type, DECODE determines which argument has the highest precedence and converts the other result or default arguments to that data type and returns that data type.

    If that data type is DECIMAL/NUMERIC and the precision and scale of the result or default argument is different, the precision and scale of the return type is set to achieve the maximum precision possible.

    For example, if the result / default argument are DECIMAL(6,3), DECIMAL(7,4), and DECIMAL(8,7), the return type would need 3 digits to the left of the decimal point and 7 digits to the right of the decimal point to avoid any reduction in precision. In this case, the return data type is set to DECIMAL(10,7).

    In cases where it is not possible to maintain the maximum precision, the data is rounded according to the DBS Control RoundHalfWayMagUp field. For example, if the result and default argument are DECIMAL(32, 8) and DECIMAL(30, 28), the return type is DECIMAL(38,14). This allows for 24 digits to the left of the decimal point required for DECIMAL(32, 8) and 14 digits to the right of the decimal point. If the DECIMAL(30,28) result or default argument is the greatest value, it is rounded to 14 places to the right of the decimal point.

    If the data type is fixed point NUMBER and the precision is less than or equal to 38, the precision and scale of the return type are calculated with the same method used for DECIMAL/NUMERIC. However, if the precision is greater than 38, the return type is changed to NUMBER(*) to avoid loss of accuracy. If the data type is floating point NUMBER, the return type is NUMBER(*).

  • If result or default is a character data type, the function returns a VARCHAR in the character set of the first result argument.