troubleshooting Question

conditional SQL output

Avatar of awalkinthepark
awalkinthepark asked on
Regular ExpressionsSQL
16 Comments1 Solution169 ViewsLast Modified:
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!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros