I need to get the available records between two DATETIME
columns in SQL Server, so that one column is the beginning and the other the end.
That is, when making a query based on both values, get the data that are in the range of the DATETIME, or that affect a part of that interval.
For example, make a query that with a start '2016-12-07 07:30'
and end '2016-12-08 10:45'
. Basically I need the query to return records that meet the following cases:
- Records that start and end within the interval.
- Records that start before the start of the interval but end within the same.
- Records that start after the start of the interval and that finish after the end of the interval.
- Records that start before the start of the interval, and end | after the end of the interval.
Basically, the goal is records that are contained at least partially or completely.
I have used the following query, but it does not work in all cases:
SELECT * FROM TABLA WHERE (VALOR_INICIO BETWEEN INICIO AND FINAL)
OR (VALOR_FINAL BETWEEN INICIO AND FINAL);