SQL 选择两个日期之间的查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14378183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:11:56  来源:igfitidea点击:

select query between two dates

sqlsql-servertsqlvisual-studio-2008

提问by Arianule

I am trying tomake a select join between two specific dates in the database

我正在尝试在数据库中的两个特定日期之间进行选择连接

This I feelis on the right path but is incorrect

我觉得这是在正确的道路上,但不正确

How can I get this to work

我怎样才能让它工作

SELECT --RTRIM(C.CustomerFirstName) + ' ' + LTRIM(C.CustomerLastName) as CustomerFullName,
       ROW_NUMBER() OVER(ORDER BY CP.ActionDate) AS RowNumber,
       C.CustomerFirstName,
       C.CustomerLastName,
       C.CustomerCompany,
       C.CustomerPosition,
       C.CustomerCountry,
       C.CustomerProvince,
       C.CustomerContact,
       CP.ActionDate,
       CP.ProductCode,
       CP.CustomerEmail
FROM   tblCustomers C
       JOIN tblCustomerProducts CP
            ON  C.CustomerEmail = CP.CustomerEmail


     ORDER BY ActionDate DESC
WHERE CP.ActionDate BETWEEN '1/17/2013' AND '19/12/2012'

回答by Mahmoud Gamal

Instead of

代替

WHERE CP.ActionDate BETWEEN '1/17/2013' AND '19/12/2012`

Try this:

尝试这个:

WHERE CP.ActionDate BETWEEN '19/12/2012' AND '1/17/2013'

Note that:This is because the BETWEENpredicate in SQL Server is Asymmetric, this means that value1 BETWEEN value2 AND value3is the same as Value1 >= Value2 AND Value1 <= Value3, so the value value2that before the ANDmust be less than or equal to the value3.

注意:这是因为BETWEENSQL Server 中的谓词是Asymmetric,这意味着value1 BETWEEN value2 AND value3与 相同Value1 >= Value2 AND Value1 <= Value3,所以value2之前的值AND必须小于或等于value3

回答by SQLMenace

The smaller date has to be listed first

必须首先列出较小的日期

BETWEEN '12/19/2012'  AND '1/17/2013'

回答by Taryn

There are a few issues with your current query.

您当前的查询存在一些问题。

First, you have the dates in the wrong order.

首先,您的日期顺序错误。

Second, you have the ORDER BYin the wrong place. The ORDER BYis the last item listed in your select:

其次,你ORDER BY在错误的地方。这ORDER BY是您选择中列出的最后一项:

FROM tblCustomers C
JOIN tblCustomerProducts CP 
   ON C.CustomerEmail = CP.CustomerEmail
WHERE CP.ActionDate BETWEEN  '12/19/2012' AND '1/17/2013'
ORDER BY ActionDate DESC

You can also use:

您还可以使用:

FROM tblCustomers C
JOIN tblCustomerProducts CP 
   ON C.CustomerEmail = CP.CustomerEmail
WHERE CP.ActionDate >= '12/19/2012' AND CP.ActionDate <= '1/17/2013'
ORDER BY ActionDate DESC

I would also advise that you be sure that the dates are formatted in the same manner. You have one as DD/MM/YYYYand the other as MM/DD/YYYY

我还建议您确保日期的格式相同。你有一个DD/MM/YYYY和另一个MM/DD/YYYY

回答by Vishal Suthar

You misplaced dates, it should be smaller one at first place and larger one at second place:

您放错了日期,它应该在第一位时较小,在第二位时应较大:

WHERE CP.ActionDate BETWEEN '19/12/2012' AND '1/17/2013'

OR

或者

WHERE CP.ActionDate >= '19/12/2012' AND CP.ActionDate <= '1/17/2013'