Sql – Ignoring a column when condition doesnt match in select query – SQL-Oracle

oraclesql

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.

Best Solution

Perhaps you want the SQL COALESCE function?

select coalesce(myMaybeNullColumn, '') from foo

will give blanks instead of nulls.

Related Question