Any way to write a query that will conditionally include a column?
I know that a Case statement can be used to control output, but
this is a little different.
For example, there is a column that contains some number of 1 character codes.
Let's say: Optionscolumn contains "ABCDEFG"
CASE WHEN Optionscolumn like '%A%' THEN 'yes' else '' END As ContainsA
CASE WHEN Optionscolumn like '%B%' THEN 'yes' else '' END As ContainsB
That words just fine, but in the case of no value, I want no column at all included.
So, if Optionscolumn is not like '%A%' then the column "ContainsA" is not in the query results at all.
Reason is that there are over 30 possible values, and I only want to include the columns for
values found, which would be around 5 or 6. I don't want 25 or so empty columns.
I've looked and tried, can't figure it out.
Since the columns needed would be different on different rows, maybe it's not possible, and
the only way is to make a temp table and drop any columns that are entirely empty?
Any thoughts?
Thanks!
Our community of experts have been thoroughly vetted for their expertise and industry experience.
This award recognizes a member of Experts Exchange who has made outstanding contributions to the community within their first year as an expert. The Rookie of the Year is awarded to a new expert who has the highest number of quality contributions.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.