在where子句中使用if-else / decode

时间:2013-11-26 05:56:40

标签: sql oracle decode

我有一个具有多个条件的游标,用于检索一组数据。我最近在其中添加了一个日期条件,以便在用户输入的特定日期之间检索数据,如下所示:

cursor c1
  is
     select  t.*
         from (select v.*, dense_rank () over (order by created desc)
                                                                  as rank
                 from test.table_v2 v
                where some condition
                and some condition
                  and some condition
                  and some condition
                  and some condition
                  and some condition
                 and ((ended) between to_date(sd,'mm/dd/rrrr')
                                  and to_date(sd,'mm/dd/rrrr')+3
                     or ended like decode(sd,null,'%')) --new condition
                 and some condition
                ) t
        where rank < rmax+1
     order by ended desc;

这里rmax = 1000

我需要在where子句中添加一个条件,这样当sd(用户输入的日期)为null时,行被限制为1000,当它不为null时,不应考虑行限制。

我不确定是否可以在where子句中使用decode。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

只需添加or sd is [not] null

即可
select  t.*
     from (select v.*, dense_rank () over (order by created desc)
                                                              as rank
             from test.table_v2 v
            where some condition
            and some condition
              and some condition
              and some condition
              and some condition
              and some condition
             and (ended between to_date(sd,'mm/dd/rrrr')
                            and to_date(sd,'mm/dd/rrrr')+3
                 or sd is null) --new condition
             and some condition
            ) t
    where rank < rmax+1 or sd is not null
相关问题