Search code examples
sqloracle-databasenull

How to replace null values with a text?


I need to display Employee last_name and their commission amount from employees table in Oracle SQL, but the condition is if it encounter NULL value I need to print "No Commission".
For the first part I wrote:

select last_name, commission_pct from employees;

But I am unable to get how to replace NULL values with "No Commission".


Solution

  • You can use case expression:

    select last_name
         , case when commision_pct is null then 'No Commission' else commision_pct end    
    from employees;
    

    or coalesce:

    select last_name
         , coalesce(commision_pct, 'No Commission')
    from employees;
    

    or nvl:

     select last_name
         , nvl(commision_pct, 'No Commission')
    from employees;
    

    P.S. In case commision_pct's datatype is not varchar you should also use cast or to_char.