简体   繁体   English

Oracle SQL 动态开始和结束日期

[英]Oracle SQL Dynamic Start and End Date

I want to have dynamic Oracle start and end date SQL that will return the start_date that is <= 2019-12-31 when Sys_Date is >= Nov 16, 2019. The YYYY portion of the criteria needs to increase by one year, every Nov 16. For example, when Sys_Date is >= Nov. 16, 2020, the query will return records having a start_date <= 2020-12-31 and on Nov. 16, 2021 the query will return records having a start date <= 2021-12-31.我想要动态 Oracle 开始和结束日期 SQL,当 Sys_Date >= 2019 年 11 月 16 日时,该 SQL 将返回 <= 2019-12-31 的 start_date。标准的 YYYY 部分需要增加一年,每年 11 月16. 例如,当 Sys_Date >= 2020 年 11 月 16 日时,查询将返回 start_date <= 2020-12-31 的记录,而在 2021 年 11 月 16 日,查询将返回起始日期 <= 2021 年的记录-12-31。

I'm using Alteryx and the IN-db tools.我正在使用 Alteryx 和 IN-db 工具。 I'm briefly familiar with these Oracle functions, SysDate, To_Date, Trunc, Add_Months, and Next_Day.我对这些 Oracle 函数、SysDate、To_Date、Trunc、Add_Months 和 Next_Day 略有熟悉。

There is an end_Date criteria that needs to advance every Nov 16 by one year as well.有一个 end_Date 标准也需要在每年 11 月 16 日提前一年。 As of today, Jan 13, 2020, The current end_date being returned is >= 2019-01-01.截至今天,即 2020 年 1 月 13 日,当前返回的 end_date 是 >= 2019-01-01。 On Nov 16, 2020 the end_Date returned will be >= 2020-01-01. 2020 年 11 月 16 日,返回的 end_Date 将 >= 2020-01-01。

This working Oracle SQL requires annual update on Nov 16.这个工作的 Oracle SQL 需要在 11 月 16 日进行年度更新。

"START_DATE" <= to_date('2019-12-31','yyyy-mm-dd') and "END_DATE" >= to_date('2019-01-01','yyyy-mm-dd')

Below is successful Alteryx code using a DateTimeNow function (not Oracle) to obtain/select/pass the desired dates, but that isn't helping me with Oracle SQL.下面是使用 DateTimeNow 函数(不是 Oracle)来获取/选择/传递所需日期的成功 Alteryx 代码,但这对我使用 Oracle SQL 没有帮助。

Below is code (not Oracle) used in the Alteryx application to query certain file dates using datetimeToday function.下面是 Alteryx 应用程序中使用的代码(不是 Oracle),用于使用 datetimeToday 函数查询某些文件日期。

([FileDt] > Datetimeformat(DatetimeAdd(DateTimeToday(),-1,"year"),'%Y-01-01')) 
AND    // pass [FileDt] > Jan 1 of current year -1 year
ToDate(DateTimeNow())< ToDate(tostring(DateTimeYear(DateTimeNow()))+"-11-15") 
  //pass files when today is < Nov 15 current year
OR
([FileDt] >= ToDate(tostring(DateTimeYear(DateTimeNow()))+"-01-01") 
AND   // pass [FileDt] >= Jan 1 of current year
ToDate(DateTimeNow())>= ToDate(tostring(DateTimeYear(DateTimeNow()))+"-11-15")) 
  // pass files when today is >= Nov 15 Current Year

Could be something like this.可能是这样的。 Maybe this is not 100% correct but you should get an idea how it works.也许这不是 100% 正确,但您应该了解它是如何工作的。

WHERE start_date <= 
   CASE WHEN TO_NUMBER(TO_CHAR(SYSDATE, 'MMDD')) < 1116 THEN 
      ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 12)-1
   ELSE 
      ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 24)-1
   END

Thank you for the input.谢谢你的反馈。 The code below is used to create a StartDate that meets my requirements:下面的代码用于创建符合我要求的 StartDate:

CASE
    WHEN SYSDATE BETWEEN 
                 TO_DATE ('1116' || (EXTRACT (YEAR FROM SYSDATE)), 'MM/DD/YYYY')
             AND TO_DATE ('1231' || (EXTRACT (YEAR FROM SYSDATE)), 'MM/DD/YYYY')
    THEN TO_DATE ('1231' || EXTRACT (YEAR FROM SYSDATE), 'MM/DD/YYYY')

    WHEN SYSDATE BETWEEN 
                 TO_DATE ('0101' || (EXTRACT (YEAR FROM SYSDATE)), 'MM/DD/YYYY')
             AND TO_DATE ('1115' || (EXTRACT (YEAR FROM SYSDATE)), 'MM/DD/YYYY')
    THEN TO_DATE ('1231' || EXTRACT (YEAR FROM SYSDATE)-1, 'MM/DD/YYY')

    ELSE NULL
END

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

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