SQL - 按日期范围查询

发布于 2024-07-25 00:00:12 字数 301 浏览 9 评论 0 原文

我有一个名为“OrderH”的订单标题表。 在此表中有一列名为“OrderDate”。 我正在尝试检索日期在特定范围内的订单。 我认为我可以使用“ Between”关键字来完成此任务,但我没有任何运气。 这就是我一直在烦恼的 SQL:

select 
    * 
from
    OrderH h
where
        h.OrderDate between '2009-06-16' and '2009-06-01'
order by
    h.OrderDate desc

我做错了什么?

I have an order header table called "OrderH". In this table there is a column called "OrderDate". I am trying to retrieve the orders with a date within a certain range. I thought that I could accomplish this with the "between" keyword but I am not having any luck. This is this SQL I have been fidgiting with:

select 
    * 
from
    OrderH h
where
        h.OrderDate between '2009-06-16' and '2009-06-01'
order by
    h.OrderDate desc

What am I doing wrong?

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

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

发布评论

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

评论(5

我不是你的备胎 2024-08-01 00:00:12

较小的日期必须放在第一位,

between  '2009-06-01' and '2009-06-16'

而不是

between '2009-06-16' and '2009-06-01'

使用 Between 时也要小心,因为您将从较大的日期中获得午夜值,而没有其他任何内容

请查看 Between 如何处理 SQL Server 中的日期?

the smaller date has to be first

between  '2009-06-01' and '2009-06-16'

instead of

between '2009-06-16' and '2009-06-01'

Also be careful when using between because you will get the midnight value from the larger date and nothing else

Take a look at How Does Between Work With Dates In SQL Server?

话少心凉 2024-08-01 00:00:12

很难找到在开始之前就结束的日期。 更改最小和最大...

h.OrderDate between '2009-06-01' and '2009-06-16'

It's hard to find dates that end before they begin. Change the min and max...

h.OrderDate between '2009-06-01' and '2009-06-16'
猫性小仙女 2024-08-01 00:00:12

查询不起作用,因为在您的示例中,第一个日期大于第二个日期。 交换日期。 第一个日期必须小于等于第二个日期。

Query does not work because in your example first date is bigger than second date. Swap the dates. First must be less than equal to second date.

赠意 2024-08-01 00:00:12
select 
    * 
from
    OrderH h
where
        h.OrderDate between '2009-06-01' and '2009-06-16'
order by
    h.OrderDate desc
select 
    * 
from
    OrderH h
where
        h.OrderDate between '2009-06-01' and '2009-06-16'
order by
    h.OrderDate desc
友欢 2024-08-01 00:00:12

在 MS-SQL Server 中,2009 年 6 月 16 日午夜之后发生的事件将不包括在内。

In MS-SQL Server events happening after 2009-06-16 at midnight will not be included.

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