0

My question is very similar to SQL Switch/Case in where clause.

I get parameters. If it is excpected that value is NULL, I need to get all the values, otherwise I need to filter with value.

I'm trying to do this in SQL:

    WHERE param 
      CASE
       WHEN #{value} IS NULL THEN {IS NOT NULL}
       ELSE =#{value}
      END

How should I correct this SQL request?

       WHERE param IS NOT NULL // all values if **value** is NULL
       WHERE param = #{value}  // i.e. value = 'data'
A user
  • 29
  • 1
  • 9

3 Answers3

1

This is normally done with logic such as this:

where #{value} is NULL or #{value} = param

I assume #{value} is the value being passed in and param is some column in a table.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

If I understand you, if the passed parameter is null then return all rows that the column is not null and if it has value then the ones that has the value. Then you should use:

where param is NOT NULL OR param = #{value}
Yahel
  • 546
  • 5
  • 6
-1

i came across similar need. found a solution. thanks to a article on https://use-the-index-luke.com/sql/where-clause/obfuscation/smart-logic

e.g. declare @param select * from blabla where ( @param is null or blabla.bla_fld = @param )

Anish Kutti
  • 337
  • 2
  • 7