Domanda

(I've found the logic of the solution here: Determine Whether Two Date Ranges Overlap)

The table structure is:

contracts[start_date, end_date] 

My form uses two inputs, inp_start_date and inp_end_date. I want to use their values to select the contracts that are in the range.

Example (pseudo):

  1. a contract is valid between its start_date to its end_date.
  2. Get all contracts that are valid, between inp_start_date to inp_end_date

What is the best way to set the conditions for this query?


edit

Examples:
if my inp_start_date is 5/21/2005
and the inp_end_date is 9/21/2005
then it will select contracts that starts and ends BEFORE AND AFTER inp_start_date and inp_end_date, as long as it was valid at that time range, even partially.

The query will find all contracts that their date range, is intersecting with the inp_start and inp_end dates range.

Contract and inputs example:

contract.start_date = 1.1.2001  |#|  contract.end_date = 5.31.2001
input.start_date = 10.21.2000   |#|  input.end_date = 2001.3.21

This contract should be caught, since at least a part of it is in the range.

È stato utile?

Soluzione

Assuming you're validating your input and that it's in the correct format, you could try the BETWEEN clause or the >= and <= operators:

SELECT id 
FROM contracts
WHERE start_date >= <inp_start_date> AND end_date <= <inp_end_date>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top