Dreamer
Dreamer

Reputation: 7551

Need a query to conditionally display column

For example, if the front end can give a Date, then add Date='someDate' with necessary AND keyword, and also show up the Date column by SELECT. Otherwise that Date column do not show either in the condition string nor in the SELECT

It is like if the Date is not null then

Select .... Date as Date01 from TableName where ....AND Date01='someDate';

if the Date is null then

Select .... from TableName where ..;

How to achieve such goal? Thank you.

Upvotes: 1

Views: 3112

Answers (1)

Taryn
Taryn

Reputation: 247680

If you want to return two separate select lists then you would need two queries to perform this.

You cannot hide a column in a SELECT list based on whether or not a date has been provided.

If you want to include the column and the condition, then you can use a case expression to provide a different value to the records that don't have the condition. Similar to this:

select 
   case when Date01='someDate' 
        then Date 
        else null end as Date01
from TableName
where yourFilters
  or Date01='someDate'

Upvotes: 3

Related Questions