How to replace null values with a text?

2019-08-16 08:03发布

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".

标签: sql oracle null
6条回答
forever°为你锁心
2楼-- · 2019-08-16 08:13

For Oracle

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

For SQL

select last_name, isnull(commission_pct,"No Commission") as commission_pct
    from employees;
查看更多
▲ chillily
3楼-- · 2019-08-16 08:18

how if the condition like this:

Select job_id, job_title, employee_id
from jobs join job_history using (job_id);

and show all of the employee_id who has been working in there, if is it not, replacing into varchar

查看更多
等我变得足够好
4楼-- · 2019-08-16 08:19

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.

查看更多
男人必须洒脱
5楼-- · 2019-08-16 08:25

It is as simple as you can see, Isnull() Used to Replace NULL values to the default value we pass there, so what i did here is If "commission_pct" having NULL value then it'll replace that with "No Commission" text, which i have passed in ISNULL() as 2nd parameter.

  select last_name, 
    ISNULL(commission_pct,'No Commission') AS commission_pct
    from employees;
查看更多
劫难
6楼-- · 2019-08-16 08:34

select Last_Name, decode(nvl(salarycommission_pct,'0'),0,'No Commission',salarycommission_pct) as COMM from employees;

查看更多
Fickle 薄情
7楼-- · 2019-08-16 08:36

Another alternative, quite simple and precise:

nvl(to_char(commision_pct), 'No Commission') 

Since, commision_pct is NUMBER data type, to_char will explicitly convert it into string.

查看更多
登录 后发表回答