下周在甲骨文

时间:2018-11-25 18:03:36

标签: oracle date-arithmetic

我正在使用oracle dbms,并且在Employe表中有一列Birthdate。我想写一个查询,显示下周生日的员工。 这是正确的吗?

select name 
from employe 
where  to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');

2 个答案:

答案 0 :(得分:2)

这不是next_day()的正确用法:该函数返回下一个 day 实例的日期。例如,要查找下一个星期五的日期:

select next_day(sysdate, 'FRIDAY') from dual;

要查找从现在起七天生日的员工,您只需稍微调整一下查询即可:

select name 
from employe
where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');

答案 1 :(得分:1)

正确的解决方案是

SELECT name
FROM employe
WHERE to_char(birthdate
              /* "move" the birthdate to the current year
                 to get a reliable week number */
              + CAST((EXTRACT(year FROM current_date)
                      - EXTRACT(year FROM birthdate)) || '-0'
                     AS INTERVAL YEAR TO MONTH),
              'IW')
    = to_char(current_date + 7, 'IW');

IW格式返回包含日期的ISO周,这可能是您要查找的日期。如果您在星期天开始一周,则在两个日期中添加一个。