See if two dates overlap

1

I have to make a query that consists of checking if the start date that they insert me and the end date that they insert me is not included in another existing article, that is:

Artículo {
Id:1
Nombre:'mesa'
F_ini: 1/7/2018
F_fin : 31/7/2018
}

So if I want to insert a chair, it can not be between 1 and 31, that is:

F_ini 29/7/2018 F_fin 8/8/2018 does not work for me because the start is understood,

The same thing happens with the end date 25/6/2018 end date - 29/7/2018 , the end date is inside the block.

Finally 1/1/2018 - 31/12/2018 It's not good either, because the other date was included inside it.

Select * from articulos where F_ini > fecha_inicio and f_fin < fecha_fin;

The problem is that this does not work for me because I can not check the 3 types of cases and I do not know how to do it ...

    
asked by EduBw 23.07.2018 в 09:47
source

1 answer

1

What you want is to check if two periods of time overlap. For that there is a very simple formula:

(FInicioA <= FFinB) and (FFinA >= FInicioB)

This formula is inclusive, that is, if the dates coincide, consider them as if they overlap, if you do not want it, change <= by < and >= by > .

In short, your query would be something like:

Select * from articulos where F_ini < fecha_fin and F_fin > fecha_inicio;
    
answered by 23.07.2018 / 10:06
source