According to cdonner in his answer here and to his blog .
He claims that BETWEEN the date gives conflicting results.
From my 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
Note that the S2 response in the second query shows "Out" when it is clear that the date should be.
According to cdonner , the reason for this is because:
[least significant digit of type DateTime in SQL is 3 ms
I think the reason is much simpler. I think this is because he used strings, not dates, in his query.
I apologize for my SQLServer-ish. I mainly talk about Oracle, so it can be ugly. But when I take his request that -proves- there is a problem 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
What is right?
NB: this is not an attempt to resolve a dispute or start a fiery war. I really want to understand if SQL Server BETWEEN is less functional than Oracle BETWEEN. We do not have such a problem in Oracle.
Mark brady
source share