简体   繁体   English

Oracle SQL - 根据当前月份定义日期的年元素

[英]Oracle SQL - Define the year element of a date dependent on the current month

I am trying to create a view in SQL Developer based on this statement:我正在尝试根据以下语句在 SQL Developer 中创建一个视图:

SELECT * FROM ORDERS WHERE START_DATE > '01-JUL-2020'

The year element of the date needs to set to the year of the current date if the current month is between July and December otherwise it needs to be the previous year.如果当前月份在 7 月和 12 月之间,则日期的 year 元素需要设置为当前日期的年份,否则需要设置为上一年。

The statement below returns the required year but I don't know how to incorporate it (or a better alternative) into the statement above:下面的语句返回所需的年份,但我不知道如何将它(或更好的替代方案)合并到上面的语句中:

select
    case
      when month(sysdate) > 6 then 
        year(sysdate)
      else
        year(sysdate)-1
    end year
  from dual

Thanks谢谢

Oracle doesn't have a built-in month function so I'm assuming that is a user-defined function that you've created. Oracle 没有内置month function 所以我假设这是您创建的用户定义的 function。 Assuming that's the case, it sounds like you want假设是这样,听起来你想要

where start_date > (case when month(sysdate) > 6
                         then trunc(sysdate,'yyyy') + interval '6' month
                         else trunc(sysdate,'yyyy') - interval '6' month
                      end)

Just subtract six months and compare the dates:只需减去六个月并比较日期:

SELECT *
FROM ORDERS
WHERE trunc(add_months(sysdate, -6), 'YYYY') = trunc(start_date, 'YYYY')

This compares the year of the date six months ago to the year on the record -- which seems to be the logic you want.这将六个月前日期的年份与记录的年份进行比较 - 这似乎是您想要的逻辑。

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

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