Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Sql query Null value returning issue with case statement

User_UMJZ5Mar 14 2017 — edited Mar 14 2017

Hi All,

I have table like below,based on some PNCODES im assiging some country codes by using case statement.im considering only 5 codes if its satisfies then value assigned if value not satisfied those records should be ignored.But here im getting null values also how to ignore null values in case statement it self.

I have used below query but im getting wrong result.Null values coming as country where ever that conditions failed.is there anyway to stop null values passing in case statement itsel.

select customer_id,case when customer_cd like 'A%' AND customer_cd<>'K0001' THEN 'USA'

WHEN customer_cd='B0001' THEN 'AUSTRALIA'

WHEN customer_cd='C0001' THEN 'NEWZEALAND'

WHEN customer_cd='E0001' THEN 'UK'

WHEN customer_cd='F0001' THEN 'UAE'

END AS COUNTRY

FROM customers

Table:

  

customer_idcustomer_cd
1A0001
2B0001
3C0001
4D0001
5E0001
6F0001
7G0001
8H0001
9I0001
10J0001
11K0001

Expected output

  

customer_idcountry
1USA
2AUSTRALIA
3NEWZEALAND
5UK
6UAE

Scripts for reference:

CREATE TABLE customer

( customer_id number(10) NOT NULL,

  customer_cd varchar2(50) NOT NULL

);

INSERT INTO customers VALUES ('1', 'A0001');

INSERT INTO customers VALUES ('2', 'B0001');

INSERT INTO customers VALUES ('3', 'C0001');

INSERT INTO customers VALUES ('4', 'D0001');

INSERT INTO customers VALUES ('5', 'E0001');

INSERT INTO customers VALUES ('6', 'F0001');

INSERT INTO customers VALUES ('7', 'G0001');

INSERT INTO customers VALUES ('8', 'H0001');

INSERT INTO customers VALUES ('9', 'I0001');

INSERT INTO customers VALUES ('10', 'J0001');

INSERT INTO customers VALUES ('11', 'K0001');

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2017
Added on Mar 14 2017
10 comments
2,109 views