Does MS SQL Server's “between” include the range boundaries?

前端 未结 8 2182
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 10:53

For instance can

SELECT foo
FROM bar
WHERE foo BETWEEN 5 AND 10

select 5 and 10 or they are excluded from the range?

8条回答
  •  悲哀的现实
    2020-11-27 11:33

    It does includes boundaries.

    declare @startDate date = cast('15-NOV-2016' as date) 
    declare @endDate date = cast('30-NOV-2016' as date)
    create table #test (c1 date)
    insert into #test values(cast('15-NOV-2016' as date))
    insert into #test values(cast('20-NOV-2016' as date))
    insert into #test values(cast('30-NOV-2016' as date))
    select * from #test where c1 between @startDate and @endDate
    drop table #test
    RESULT    c1
    2016-11-15
    2016-11-20
    2016-11-30
    
    
    declare @r1 int  = 10
    declare @r2 int  = 15
    create table #test1 (c1 int)
    insert into #test1 values(10)
    insert into #test1 values(15)
    insert into #test1 values(11)
    select * from #test1 where c1 between @r1 and @r2
    drop table #test1
    RESULT c1
    10
    11
    15
    

提交回复
热议问题