February 2010 (1)
September 2009 (1)
May 2009 (1)
April 2009 (1)
March 2009 (4)
January 2009 (3)
November 2008 (2)
October 2008 (2)
September 2008 (1)
August 2008 (5)
July 2008 (3)
June 2008 (1)
May 2008 (5)
April 2008 (8)
March 2008 (3)
February 2008 (1)
January 2008 (2)
December 2007 (2)
November 2007 (4)
October 2007 (17)
September 2007 (9)
Tuesday, May 13 2008
I love a good bit of language lawyering as much as the next programmer, but perhaps not so much when I am the victim, as opposed to the smug bastard who remembers all the footnotes and can do a good Nelson Muntz-style “ha ha!”
It turns out that in SQL, the BETWEEN AND comparison operator does not work quite the way you would expect. Quoting the Postgres manual:
a BETWEEN x AND yis equivalent to
a >= x AND a <= ySimilarly,
a NOT BETWEEN x AND yis equivalent to
a < x OR a > y
Now, if perchance x is greater than y — for example, if you write SELECT foo FROM bar WHERE foo BETWEEN 200 AND 100 — SQL will treat that as if you had written SELECT foo FROM bar WHERE foo >= 200 AND foo <= 100, and return nothing, thus doing the complete opposite of what you might expect from the normal English meaning of “between”.
This has tripped up both me and a colleague in the last couple of weeks.
Not so much syntactic sugar as syntactic Nutrasweet…
Tags: sql ~ programming ~ annoyance
Rendered at 2012-02-10 00:37:22