According to cdonner, in his answer here and on his blog.
He claims that BETWEEN with date yields inconsistent results
From his blog:
select
case when '9/1/08' between '9/1/08' and '9/15/08'
then 'in' else 'out' end as s1,
case when '9/1/08' between '8/28/08' and '9/1/08'
then 'in' else 'out' end as s2
s1 s2
---- ----
in in
(1 row(s) affected)
select
case when '1/1/08' between '1/1/08' and '2/1/08'
then 'in' else 'out' end as s1,
case when '1/1/08' between '12/31/07' and '1/1/08'
then 'in' else 'out' end as s2
s1 s2
---- ----
in out
(1 row(s) affected
Notice that the S2 answer in the second query show "Out" when clearly the date should be in.
According to cdonner, the reason this is so is because:
[the] least significant digit of the DateTime type in SQL is 3 msec
I think the cause is much simpler than that. I think it's because he's using strings and not dates in his query.
Please excuse my SQLServer-ish. I speak mainly Oracle, so this may be ugly. But when I take his query that -proves- there's an issue and replace his strings with datetime variables I get the correct output.
DECLARE @Jan108 datetime
DECLARE @Feb108 datetime
DECLARE @Dec3107 datetime
SET @Jan108 = '1/1/08'
SET @Feb108 = '2/1/08'
SET @Dec3107 = '12/31/07'
select
case when @Jan108 between @Jan108 and @Feb108
then 'in' else 'out' end as s1,
case when @Jan108 between @Dec3107 and @Jan108
then 'in' else 'out' end as s2
Which is correct?
NB: this isn't an attempt to settle an argument or to start a flame war. I truly want to understand if SQL Server BETWEEN is less functional than the Oracle BETWEEN. WE have no such issue in Oracle.