SQL WHERE 日期范围查询

发布于 2024-12-19 08:31:49 字数 550 浏览 1 评论 0原文

在我的表中,有 118 条详细说明项目的记录。我在这里关心的两个字段是开始日期和结束日期。

我需要从此视图生成一份报告,显示哪些项目在以下日期范围内处于“活动”状态:

01/01/2011 - 01/12/2011

我尝试了以下 WHERE 类:

WHERE startdate BETWEEN '01/04/2011' AND '01/12/2011' 
OR enddate BETWEEN '01/04/2011' AND '01/12/2011'
OR startdate <= '01/04/2011' AND enddate >= '01/12/2011'

出现的情况似乎不正确,仅显示了几条记录,而我知道应该显示的许多记录并未显示,例如一个项目的开始日期为 20/07/2011,结束日期为运行 WHERE 查询时,21/11/2011 会消失。

任何人都可以看到此 WHERE 查询的错误

在此处输入图像描述

In my table I have 118 records detailing projects. The 2 fields I am concerned with here are startdate and enddate.

I need to produce a report from this view which shows which projects were 'active' between the following date ranges:

01/01/2011 - 01/12/2011

I have tried the following WHERE clase:

WHERE startdate BETWEEN '01/04/2011' AND '01/12/2011' 
OR enddate BETWEEN '01/04/2011' AND '01/12/2011'
OR startdate <= '01/04/2011' AND enddate >= '01/12/2011'

What comes through does not seem correct, there are only a few records displayed and many which I know for a fact should be displayed are not, such as one project with a start date of 20/07/2011 and enddate of 21/11/2011 dissapears when the WHERE query is run.

Can anyone see a fault with this WHERE query

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨洒年华 2024-12-26 08:31:49
WHERE
    startdate <= '2011-12-01'
AND enddate   >= '2011-01-01'

(假设 enddate 中的值是项目活动的最后日期)

使用数字的示例,搜索任何重叠 100 到 200 的内容...

Start | End | Start <= 200 | End >= 100

 000  | 099 |  Yes         | No
 101  | 199 |  Yes         | Yes     (HIT!)
 201  | 299 |  No          | Yes
 000  | 150 |  Yes         | Yes     (HIT!)
 150  | 300 |  Yes         | Yes     (HIT!)
 000  | 300 |  Yes         | Yes     (HIT!)

这绝对需要逻辑中的 AND :)

就您的查询而言.. 。

带括号的查询看起来像这样...

WHERE
  (
     startdate BETWEEN '01/04/2011' AND '01/12/2011'
  OR enddate   BETWEEN '01/04/2011' AND '01/12/2011'
  OR startdate <= '01/04/2011'
  )
  AND enddate >= '01/12/2011'

但是您的示例永远不会满足最后一个 AND 条件。尝试添加括号以更明确...

WHERE
     (startdate BETWEEN '01/04/2011' AND '01/12/2011')
  OR (enddate   BETWEEN '01/04/2011' AND '01/12/2011')
  OR (startdate <= '01/04/2011' AND enddate >= '01/12/2011')
WHERE
    startdate <= '2011-12-01'
AND enddate   >= '2011-01-01'

(Assuming the value in enddate is the last date the project is active)

Examples using numbers, searching for anything that overlaps 100 to 200...

Start | End | Start <= 200 | End >= 100

 000  | 099 |  Yes         | No
 101  | 199 |  Yes         | Yes     (HIT!)
 201  | 299 |  No          | Yes
 000  | 150 |  Yes         | Yes     (HIT!)
 150  | 300 |  Yes         | Yes     (HIT!)
 000  | 300 |  Yes         | Yes     (HIT!)

This absolutely needs an AND in the logic :)

In terms of your query...

Your query with parenthesis, looks like this...

WHERE
  (
     startdate BETWEEN '01/04/2011' AND '01/12/2011'
  OR enddate   BETWEEN '01/04/2011' AND '01/12/2011'
  OR startdate <= '01/04/2011'
  )
  AND enddate >= '01/12/2011'

But your example never meets the last AND condition. Try adding parenthesis to be more explicit...

WHERE
     (startdate BETWEEN '01/04/2011' AND '01/12/2011')
  OR (enddate   BETWEEN '01/04/2011' AND '01/12/2011')
  OR (startdate <= '01/04/2011' AND enddate >= '01/12/2011')
失而复得 2024-12-26 08:31:49

在查询添加之前

set dateformat dmy

还可以添加一些括号

WHERE 
(startdate BETWEEN '01/01/2011' AND '01/12/2011')
OR 
(enddate BETWEEN '01/01/2011' AND '01/12/2011')
OR 
(startdate <= '01/01/2011' AND enddate >= '01/12/2011')

Before the query add

set dateformat dmy

Also maybe add some brackets

WHERE 
(startdate BETWEEN '01/01/2011' AND '01/12/2011')
OR 
(enddate BETWEEN '01/01/2011' AND '01/12/2011')
OR 
(startdate <= '01/01/2011' AND enddate >= '01/12/2011')
左秋 2024-12-26 08:31:49

假设 startdateenddate 是日期字段,

请尝试以下操作:

WITH Dates AS ( 
      SELECT [Date] = @StartDate
      UNION ALL SELECT [Date] = DATEADD(DAY, 1, [Date])
      FROM Dates WHERE [Date] <= DATEADD(DAY, -1, @EndDate)
) 
-- YOUR SELECT STATEMENT
-- YOUR FROM STATEMENT
CROSS APPLY Dates
WHERE [Date] BETWEEN startdate AND enddate 
-- The rest of your where statement here
OPTION(MAXRECURSION 0);

将开始日期声明为 01/01/2011,将结束日期声明为 2011年1月12日

Assuming startdate and enddate are date fields,

Try this:

WITH Dates AS ( 
      SELECT [Date] = @StartDate
      UNION ALL SELECT [Date] = DATEADD(DAY, 1, [Date])
      FROM Dates WHERE [Date] <= DATEADD(DAY, -1, @EndDate)
) 
-- YOUR SELECT STATEMENT
-- YOUR FROM STATEMENT
CROSS APPLY Dates
WHERE [Date] BETWEEN startdate AND enddate 
-- The rest of your where statement here
OPTION(MAXRECURSION 0);

Declaring your start date as 01/01/2011 and your end date as 01/12/2011

江挽川 2024-12-26 08:31:49

当您可以将字符串与列进行比较时,每个人都会以错误的方式将开始日期和结束日期与字符串进行比较;以下是确定您想要什么的最简单方法:

where ( '01/04/2011' between startdate and enddate
          or  '01/12/2011' between startdate and enddate
         )

Everyone's looking at this the wrong way round comparing startdate and enddate to a string when you can compare the string to the columns; the following is the simplest way of ascertaining what you want:

where ( '01/04/2011' between startdate and enddate
          or  '01/12/2011' between startdate and enddate
         )
生生不灭 2024-12-26 08:31:49

我的原始查询正常工作,但我连接的数据库与我的查询具有不同的日期格式。

My original query was working, the database I was connecting to however had different date formats to my query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文