简体   繁体   English

在Oracle 12c中查找日期范围内的所有记录

[英]Finding all records within a date range in Oracle 12c

I'm not sure why my syntax isn't working, but I'm looking for all records where the field Pend_Start_Dt is greater than my variable PndStDt and Pend_End_Date is less than my variable PndEnDt. 我不确定为什么我的语法不起作用,但是我正在寻找字段Pend_Start_Dt大于变量PndStDt且Pend_End_Date小于变量PndEnDt的所有记录。 If the record has no current end date, then it should pull that too, as long as the start date is between the start and end date the user enters. 如果记录没有当前的结束日期,那么只要开始日期在用户输入的开始日期和结束日期之间,记录也应拉该日期。

So, basically, there's 2 scenarios where the record should be included: 因此,基本上,在两种情况下都应包含记录:

The Start Date field is greater than or equal to the start date the user enters and the End Date field is less than or equal to the end date the user enters “开始日期”字段大于或等于用户输入的开始日期, “结束日期”字段小于或等于用户输入的结束日期

or 要么

the Start Date field is between the start date and end date the user enters and the End Date field is null. “开始日期”字段位于用户输入的开始日期和结束日期之间 “结束日期”字段为null。

I hope that's not too confusing. 我希望不要太困惑。

The syntax I tried is below, but my stored procedure won't compile. 我尝试过的语法如下,但是我的存储过程无法编译。

Where a.Audit_Status = '3' 
AND ((a.Pend_Start_DT >= TO_DATE (PndStDt, 'yyyy/mm/dd')) AND (a.Pend_End_DT <= (PndEnDT, 'yyyy/mm/dd')) 
    OR ((a.Pend_Start_DT between TO_DATE (PndStDt, 'yyyy/mm/dd') and TO_DATE (PndEnDT, 'yyyy/mm/dd') AND a.Pend_End_DT is null));

There's a missing TO_DATE and the parens are not in the correct place: 缺少TO_DATE ,并且括号未放在正确的位置:

WHERE a.Audit_Status = '3'
AND
( 
   ( a.Pend_Start_DT    >= To_Date( PndStDt, 'yyyy/mm/dd' )
      AND a.Pend_End_DT <= To_Date( PndEnDT, 'yyyy/mm/dd' )
   )
   OR
   ( a.Pend_Start_DT BETWEEN To_Date( PndStDt, 'yyyy/mm/dd' ) 
      AND To_Date( PndEnDT, 'yyyy/mm/dd' )
      AND a.Pend_End_DT IS NULL
   ) 
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM