SQL : BETWEEN vs <= 和 >=

2021-12-10 00:00:00 sql where between tsql sql-server

在 SQL Server 2000 和 2005 中:

In SQL Server 2000 and 2005:

  • 这两个 WHERE 子句有什么区别?
  • 我应该在哪些场景中使用哪个?

查询 1:

SELECT EventId, EventName
FROM EventMaster
WHERE EventDate BETWEEN '10/15/2009' AND '10/18/2009'

查询 2:

SELECT EventId, EventName
FROM EventMaster
WHERE EventDate >='10/15/2009'
  AND EventDate <='10/18/2009'

(最初缺少第二个 Eventdate,因此查询在语法上是错误的)

( the second Eventdate was originally missing, so the query was syntactically wrong)

推荐答案

它们是相同的:BETWEEN 是问题中较长语法的简写,其中包含两个值 (EventDate >= '10/15/2009' 和 EventDate <= '10/19/2009').

They are identical: BETWEEN is a shorthand for the longer syntax in the question that includes both values (EventDate >= '10/15/2009' and EventDate <= '10/19/2009').

使用另一种更长的语法,其中 BETWEEN 不起作用,因为不应包含一个或两个值,例如

Use an alternative longer syntax where BETWEEN doesn't work because one or both of the values should not be included e.g.

Select EventId,EventName from EventMaster
where EventDate >= '10/15/2009' and EventDate < '10/19/2009'

(注意 < 而不是 <= 在第二个条件中.)

(Note < rather than <= in second condition.)

相关文章