views:

301

answers:

2

Hi,

My requirement is to display a Column in select query when a condition satisfies and not to display the column when condition is not matched.

For eg : In this simple table below

Table : XYZ

Name ID Fairness

harish 3 White

ravi 5 brown

arun 2 black

rahul 5 white

Query:

select name,case id when 5 then " I Like to learn more languages" end as Remarks, Fairness from xyz where id=2

My requirement is in the above query "Remarks" column should not be displayed in output, but my output is

Actual Output:

Name Remarks Fairness

arun null black

Expected Output:

Name Fairness

arun black

i.e, I need remarks column to be displayed only if the id is 5 in where clause.

Please provide me help to ignore "Remarks" when the condition is not satisfied or met.

Thanks,

A: 

Perhaps you want the SQL COALESCE function?

select coalesce(myMaybeNullColumn, '') from foo

will give blanks instead of nulls.

Jonathan Feinberg
A: 

Just add an else condition to your query to return blank for those cases you don't want to show.

select name,case id when 5 then " I Like to learn more languages" else "" end as Remarks, Fairness from xyz where id=2
pedromarce
he said "i.e, I need remarks column to be displayed only if the id is 5 in where clause" if he add "else "" " he would see the column which is empty
Donar
True, I misunderstood. but then without a stored procedure to achieve that in a select it is just impossible.Maybe I would add the remarks to an existing column rather to create a new one (if that fulfill the requirements of course).
pedromarce
yeah full acc, on the other side i don't see his problem, because if call this select out of a prgrammcode, he could check the content of the second column there. and in pl/sql it is not really a problem
Donar