You are here

Coalesce function in Teradata

Submitted by Asif Nowaj, Last Modified on 2019-12-23

In Teradata Coalesce function returns NULL if all its arguments evaluate to null. Otherwise, it returns the value of the first non-null argument in the scalar_expression list.

COALESCE is a shorthand expression for the following full CASE expression:

   CASE 
    WHEN scalar_expression_1 IS NOT NULL 
    THEN scalar_expression_1 
    ...
    WHEN scalar_expression_n IS NOT NULL 
    THEN scalar_expression_n 
    ELSE NULL 
   END

teradata-case-statement

Example of Teradata COALESCE function

The following example returns the home phone number of the named individual (if present), or office phone if HomePhone is null, or MessageService if present and both home and office phone values are null. Returns NULL if all three values are null.

   SELECT Name, COALESCE (HomePhone, OfficePhone, MessageService) 
   FROM PhoneDir;

Example of Teradata COALESCE function

The following example uses COALESCE with an arithmetic operator.

   SELECT COALESCE(Boxes,0) * 100
   FROM Shipments;

Example of Teradata COALESCE function

The following example uses COALESCE with a comparison operator.

   SELECT Name
   FROM Directory
   WHERE Organization  COALESCE (Level1, Level2, Level3);

Discussion or Comment

If you have anything in mind to share, please bring it in the discussion forum here.

https://forum.everyething.com/others-f41/