Hi, I hope you're ok, is there any way to adapt an SQL statement so that it reacts according to whether a parameter arrives nil or not, something like this:
drop table if exists tabla;
create table tabla (
id int primary key AUTO_INCREMENT,
fecha date not null
);
insert into tabla (fecha) values (STR_TO_DATE('24-May-2018', '%d-%M-%Y'));
SET @fecha_radicado = STR_TO_DATE('24-May-2018', '%d-%M-%Y');
SELECT @fecha_radicado;
SELECT *
from tabla as t
where (
CASE @fecha_radicado
WHEN IS NOT NULL THEN t.fecha = @fecha_radicado
WHEN IS NULL THEN t.fecha is NULL
END;
) as fe;
Note that the query has the CASE, takes an action depending on the parameter that arrives, is there any operator that allows to perform this type of conditions within the CRUD statements when it occurs that one does not know if the parameter is null or has something? Yes, there are solutions like these:
- make a judgment for each case, which would be completely inefficient because if 20 parameters arrive, it would be almost (20 C 2) approximately 190 sentences that it would have to carry out.
- Make a method already in a language like php, c # or whatever it is that allows me to create a sentence that suits the need and then it is executed.
But I appreciate if anyone knows if something like that can be done within a select. THANK YOU!!
NOTE: I understand that there are mutiles SQL engines, but as I understand there is the ANSI scheme, so I put the question open, however I would be more interested if it is for MySQL.
To test: rextester.com/JZTO58225