Skip to content
Advertisement

Is there an SQL function that let us put condition above WHERE clause?

I have some query with several WHERE clauses. Based on an external variable :flag, I would like to set a condition when I can use a WHERE clause. Below is a presentation of what I mean. There is a pseudo if else statement which I’d like to have.

Advertisement

Answer

You can use regular boolean logic for this:

This form assumes that :flag is not NULL. It can be tweaked to handle that situation.

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