What is the use of the VALUE function in a DB2? Explain with the help of an example

DB2DatabaseBig Data Analytics

The purpose of VALUE function in DB2 is to check for NULL values and it can be used in place of NULL indicator or COALESCE function. The VALUE function replaces the column value with the given argument if it contains a NULL value.

For example, if we have an ORDER table and we have to extract ORDER_ID and ORDER_DESCRIPTION from this table. The ORDER_DECRIPTION column can have NULL values.

If this is the case, we have to replace ORDER_DESCRIPTION with SPACES, then we can use the below query:

Example

EXEC SQL
   SELECT ORDER_ID, VALUE(ORDER_DESCRIPTION, ‘ ‘)
   INTO :ORDER-ID, :ORDER-DESCRIPTION
   FROM ORDERS
END-EXEC

In this case, the VALUE function will take two arguments. The first one is the name of the column and second one is the expected value if the column contains NULL values.

raja
Published on 30-Nov-2020 08:55:44
Advertisements