Using CAST to Avoid Numeric Overflow - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
ft:locale
en-US
ft:lastEdition
2024-12-11
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

Because of the way the Teradata SQL compiler works, you CAST must the arguments of your expressions whenever large values are expected.

For example, suppose f1 is defined as DECIMAL(14,2) and you are going to multiply by an integer or get SUM(f1).

The following operations:

CAST(f1 AS DECIMAL(18,2))*100

or

SUM(CAST(f1 AS DECIMAL(18,2)))

are proper techniques for ensuring correct answer sets.

But if you cast the results of the expressions, such as the following:

CAST(f1*100 AS DECIMAL(18,2))

or

CAST(SUM(f1) AS DECIMAL(18,2)

overflow during the computations (and before the CAST is made) is likely—not the desired result.