Bring all records if parameter is different

3

I have a contrato table with the concluido field of type tinyint(1) , if it is 0 it is not complete and if it is 1 it is complete. I'm going to pass this value by parameter to the query, how do I get all the data when the value of this parameter is different from 0 and 1?

    
asked by anonymous 07.12.2016 / 20:27

2 answers

3

Use the OR clause for conditional search.

SELECT c.*
  FROM contrato c
 WHERE :parametro NOT IN (0, 1)
    OR c.concluido = :parametro;

If the parameter can be null, use one more clause OR :

SELECT c.*
  FROM contrato c
 WHERE :parametro IS NULL
    OR :parametro NOT IN (0, 1)
    OR c.concluido = :parametro;
    
07.12.2016 / 20:30
1

Use "coalesce" (default on all sql banks) to save an "or" if your parameter is null too.

SELECT * From contract ONDE  completed =: parameter  or  coalesce (: parameter, -1) not in (0, 1);

Another way would be this:

SELECT * From contract ONDE  completed = case when coalesce (: parameter, -1) between 0 and 1 then: parameter else completed end

    
09.12.2016 / 04:16