Attilah
Attilah

Reputation: 17930

how to process a column and return it as part of a query?

I have a table Part(No,Desc,Draw).

I want to return a list of all the parts in the table in this form :

Part Number| Description | Is Packed ? (Yes/No)|
------------------------------------------------

In the result set, the Is Packed header should be Yes if the Draw column is not null and No if otherwise.

I'm using PL/SQL.

I don't know how to do this.

Upvotes: 0

Views: 68

Answers (3)

Mark Byers
Mark Byers

Reputation: 838226

Use CASE:

CASE WHEN Draw IS NOT NULL THEN 'Yes'
     ELSE 'No'
END AS column_alias

Upvotes: 1

Yahia
Yahia

Reputation: 70369

As an alternative to CASE (see other answers) in this case you can use NVL2:

SELECT No AS "Part Number", Desc AS "Description", NVL2 ( DRAW, 'YES', 'NO' ) AS "Is Packed" FROM PART

BTW: you should neve create a DB object (table/column...) with a name which is a reserved word (in you case Desc) - this can lead to really strange things...

Upvotes: 2

a1ex07
a1ex07

Reputation: 37364

SELECT No AS "Part Number", Desc AS "Description", 
CASE 
WHEN Draw IS NOT NULL THEN 'Yes'
ELSE 'No'
END AS "Is Packed"
FROM Part

You might need to escape Desc in field list since it's a reserved word.

Upvotes: 4

Related Questions