How to replace null values with a text? - sql

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

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.

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;

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.

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;

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

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

Related

What is wrong with my Postgres statement?

SELECT first_name, last_name, manager_id
CASE manager_id
WHEN manager_id IS null THEN "pip"
End manager_id
FROM assgnssql.employees;
I am trying to select list of employees, but i know some employees do not have manager_id, for these employees without manager_id (null) i want the result to display "pip" while for the rest it displays original info.
The code you want is probably:
SELECT first_name, last_name, manager_id
(CASE WHEN manager_id IS null THEN 'pip' ELSE manager_id
END) as manager_id
FROM assgnssql.employees;
Or more simply:
SELECT first_name, last_name, manager_id
COALESCE(manager_id, 'pip') as manager_id
FROM assgnssql.employees;
The two significant issues are:
Your CASE syntax is messed up. Either you use comparisons or you have CASE <value>, but not both.
Strings are delimited by single quotes.

How to check for NULLs in Oracle 'CASE' Expression? [duplicate]

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".
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.
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;
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.
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;
select Last_Name,
decode(nvl(salarycommission_pct,'0'),0,'No Commission',salarycommission_pct) as COMM
from employees;
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

SQL for Mixed Case filter

I was just wondering how you would search for any mix of case. For instance, I want to find all employees with the last name 'davies', but I want to be able to find any mix of case such as 'DavIes' or 'DAVies'. This is what I've tried.
SELECT LAST_NAME
FROM EMPLOYEES
WHERE DEPARTMENT_ID = (SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = '[Dd][Aa][Vv][Ii][Ee][Ss]');
I would use UPPER or LOWER function and IN
SELECT LAST_NAME
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (
SELECT DEPARTMENT_ID
FROM EMPLOYEES
WHERE UPPER(LAST_NAME) = 'DAVIES'
);
Unfortunately it invalidates the index use.
Using UPPER
SELECT *
FROM EMPLOYEES
WHERE UPPER(LAST_NAME) = 'DAVIES'

How do I correct this script

I want to be able to produce results that
If there is a commission it produces it
If there is not a commission it produces the manager_id
If there is no manager_id or commission it produces -1
I'm thinking I might have to do NLV2(NVL2(NVL2))) but I am unsure. This is the code I produced I'm sureI'm wrong.
SELECT first,
last_name,
CASE commission_pct
WHEN commission_pct IS NOT NULL THEN commision_pct
WHEN commission_pct IS NULL THEN manager_id
WHEN commission_pct AND manager_id IS NULL THEN -1
END AS "Which Function???"
FROM employees
You are looking for COALESCE which returns the first non-null value:
select
first_name,
last_name,
coalesce(commission_pct, manager_id, -1)
from employees;

select statement oracle

I'm learning oracle sql from oracle sql fundamentals book, and i found this quiz, the answer as Oracle says 2,3 but they don't work on the sql developers
I know that they have to be like this
SELECT first_name, last_name, job_id, salary*12 "yearly_sal" FROM employees;
SELECT first_name, last_name, job_id, salary AS "yearly_sal" FROM employees;
but Oracle gives the answer as shown in the image below...
None of those answers are correct. Embedded spaces are only valid as column aliases if the alias is delimited by ". Also, + cannot be used to concatenate (what I assume to be) strings - instead you can use ||. They could be corrected as:
1.
SELECT first_name, last_name, job_id, salary*12 AS "Yearly Sal" FROM employees;
2.
SELECT first_name, last_name, job_id, salary*12 "yearly sal" FROM employees;
3.
SELECT first_name, last_name, job_id, salary AS "yearly sal" FROM employees;
4.
SELECT first_name || last_name AS name, job_Id, salary*12 "yearly sal" FROM employees;