What is the function of COALESCE()?

Question

When do we use Coalesce() function?

in progress 0
Shashi Jain 4 years 3 Answers 1116 views Newbie 0

Answers ( 3 )

  1. COALESCE()is used when we want to return a default value when there is null in the the table
    Eg: Select revenue – Coalesce (cost price, 0) as profit from table_name

  2. Any amateur SQL person would think Why to use COALESCE , when we have ISNULL function in SQL. This is because when we have multiple values , and not sure they might be null , then we opt for COALESCE. It return the first non-null value in a list.

    For e.g.

    emp_vehicle_count = NULL
    emp_mobile_count = NULL
    emp_count = 200
    SELECT COALESCE(emp_vehicle_count, emp_mobile_count,emp_count);
    Output : 200

  3. Coalesce() function is used to handle NULL values.
    During the evaluation process, the NULL values are replaced with the user-defined value.

Leave an answer

Browse
Browse