Does BETWEEN with dates actually work in SQL SERVER 2008

后端 未结 8 903
旧时难觅i
旧时难觅i 2021-01-20 00:36

According to cdonner, in his answer here and on his blog.

He claims that BETWEEN with date yields inconsistent results

From his blog:

<
8条回答
  •  再見小時候
    2021-01-20 01:07

    Because he's totally wrong - it's comparing strings

    if you cast them to a datetime or replace them with date variables it works:

    select
        case when CAST('JAN 01 2008' as smalldatetime)
            between CAST('JAN 01 2008' as smalldatetime)
            and CAST('FEB 01 2008' as smalldatetime)
            then 'in' else 'out' end as s1,
        case when CAST('JAN 01 2008' as smalldatetime)
            between CAST('DEC 31 2007' as smalldatetime)
            and CAST('JAN 01 2008' as smalldatetime)
            then 'in' else 'out' end as s2
    

提交回复
热议问题