SQL statement to locate if a date is between 2 others

0

I want to be able to find all the records with value 1 in attendance that are between 2 dates for a user:

SELECT * FROM asistencia 
WHERE id='124' AND asistencia = '1' AND fecha BETWEEN '2018-07-30' AND '2018-08-12'

And as an additional filter if in this case $fecha_de_ingreso='2018-08-08'; shows me the 4 corresponding records, SI $fecha_de_ingreso is not between the two dates, that does not show me anything.

    
asked by claus 15.08.2018 в 16:41
source

2 answers

1

Hello I see that you are filtering by a ID 124, in that case it will only bring you a record, to validate if the date entered or not between the range of dates I advise you to pass the dates to a BIND and make them to_date something like this ...

SELECT * FROM asistencia 
WHERE asistencia = '1' 
    AND fecha BETWEEN to_date(:fechaInicialRango,'YYYY-MM-DD') 
    AND to_date(:fechFinalRango,'YYYY-MM-DD') 
    AND fecha_ingreso = to_date(:fechIngreso,'YYYY-MM-DD');

If it's not what you need, give me a little more detail to help you.

    
answered by 15.08.2018 / 16:51
source
1

I do not understand your question very well but the first sentence you should write it in the following way so that it does not affect the date format of the computer, in case you change it or execute it in another device:

SELECT * FROM asistencia where id='124' AND asistencia = 1 AND fecha BETWEEN '20180730' AND '20180812'

In case you want the date to be in the previous range and also the date of entry is within the first filter (that's how I understood it), then your sentences would be like this

SELECT * FROM asistencia where id='124' AND asistencia=1 AND (fecha BETWEEN '2018-07-30' AND '2018-08-12') AND fecha IN ('20180808')
    
answered by 15.08.2018 в 16:58