SQL oracle如何结合两个条件

时间:2017-09-27 09:08:12

标签: sql oracle

如何将这两个条件合并为一个?

select ename, hiredate, next_day(last_day(hiredate)-7,5)
from emp
where to_number(to_char(hiredate, 'DD)) < 15 ORDER BY hiredate ASC;

select ename, hiredate, last_day(next_day(last_day(hiredate), 5))
from emp 
where to_number(to_char(hiredate, 'DD)) > 15 ORDER BY hiredate ASC;

我有一个想法是将这两个存储到变量中,然后将它们连接起来。 但是,我目前是sql oracle的新手,我不知道该怎么做。 也许我可以做点什么?我希望你能理解我想要完成的事情。

Define variable1 = next_day(last_day(hiredate)-7,5)
where to_number(to_char(hiredate, 'DD)) < 15

1 个答案:

答案 0 :(得分:2)

您可以使用CASE表达式根据雇用日期确定要呈现的日期。

select
    ename,
    hiredate,
    case when to_number(to_char(hiredate, 'DD')) < 15
         then next_day(last_day(hiredate)-7,5)
         when to_number(to_char(hiredate, 'DD')) > 15
         then last_day(next_day(last_day(hiredate), 5)) end AS date
from emp
order by hiredate ASC;

请注意,目前尚不清楚为何不提供以下可能性:

to_number(to_char(hiredate, 'DD')) = 15

也许你应该用另外两个条件之一来解决它。