1

I want to check if a certain room is available between a start and end date. I am doing this with a webservice function. This function gives back a list with all the reservations of a given room. If this list is empty, the room is available, else it's not. For know I have this query. But it always gives back all my reservations.

SELECT * FROM ARTICLES_RESERVERING res
INNER JOIN ARTICLES_ZAAL roo ON res.ZAALID = roo.ID 
WHERE roo.ID = @ZAALID
AND res.DATUM_BEGIN >= @DATUM_EINDE OR res.DATUM_EINDE <= @DATUM_BEGIN

Could anybody help ?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Steaphann
  • 2,797
  • 6
  • 50
  • 109

2 Answers2

2

AND has higher precedence than OR.

Change the last line to:

AND (res.DATUM_BEGIN >= @DATUM_EINDE OR res.DATUM_EINDE <= @DATUM_BEGIN)
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
0
  SELECT * FROM ARTICLES_RESERVERING res
  INNER JOIN ARTICLES_ZAAL roo ON res.ZAALID = roo.ID 
  WHERE roo.ID = @ZAALID
  AND  @DATUM_EINDE between res.DATUM_BEGIN and res.DATUM_EINDE

I will rather use this query as using OR is generally expensive.

ejb_guy
  • 1,125
  • 6
  • 6
  • It was not part of the question to get it efficient; maybe that is a good thing to achieve, but your answer is just wrong. – Argeman May 29 '12 at 17:13
  • @Argeman Well given query gives the desired result. That what was asked. There are diff ways to correct the query – ejb_guy May 29 '12 at 17:56
  • Oh, now i see! Sorry, my fault, i have missed the point totally with my comment. – Argeman May 29 '12 at 19:06