Skip to content
Advertisement

Show column according to parameter

I’m trying to write a SQL script that shows a column based on a user parameter. I can’t find an acceptable answer anywhere. 

This is a simplified version of what I want to do (in OracleDB):

The pseudo code of what I want could be

This is the expected result:

Advertisement

Answer

You can just put the logic into the where clause, with or; something like:

If your column was nullable you might want additional logic to handle that in a particular way, but it doesn’t look like this should be. And if you need other conditions in the where, remember to put parentheses around the logic above to make sure it’s interpreted as you intend; i.e.

As an aside, you might want to consider using table aliases to reduce the typing a bit.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement