sql >> Base de Datos >  >> RDS >> Oracle

¿Cómo obtener la fecha de vigencia actual en Oracle?

No lo intente, pero compruébelo usted mismo :-)
Y con solo un acceso a la mesa.

Tu mesa:

SQL> create table mytable (tid,tname,effectivedate)
  2  as
  3  select 1, 'A', date '2011-07-01' from dual union all
  4  select 2, 'A', date '2011-08-01' from dual union all
  5  select 3, 'A', date '2011-09-01' from dual union all
  6  select 4, 'A', date '2011-10-01' from dual union all
  7  select 5, 'B', date '2011-08-01' from dual union all
  8  select 6, 'B', date '2011-09-01' from dual union all
  9  select 7, 'B', date '2011-10-01' from dual union all
 10  select 8, 'C', date '2011-09-01' from dual
 11  /

Table created.

Y sus dos casos de prueba:

SQL> var TODAY varchar2(10)
SQL> exec :TODAY := '2011-09-10'

PL/SQL procedure successfully completed.

SQL> select tid
  2       , tname
  3       , effectivedate
  4       , case
  5         when  to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
  6           and to_date(:TODAY,'yyyy-mm-dd') < next_effectivedate
  7         then
  8           'Valid'
  9         when to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
 10         then
 11           'Invalid'
 12         else
 13           'Inactive'
 14         end status
 15    from ( select tid
 16                , tname
 17                , effectivedate
 18                , lead(effectivedate,1,date '9999-12-31') over (partition by tname order by effectivedate) next_effectivedate
 19             from mytable
 20         )
 21  /

       TID T EFFECTIVEDATE       STATUS
---------- - ------------------- --------
         1 A 01-07-2011 00:00:00 Invalid
         2 A 01-08-2011 00:00:00 Invalid
         3 A 01-09-2011 00:00:00 Valid
         4 A 01-10-2011 00:00:00 Inactive
         5 B 01-08-2011 00:00:00 Invalid
         6 B 01-09-2011 00:00:00 Valid
         7 B 01-10-2011 00:00:00 Inactive
         8 C 01-09-2011 00:00:00 Valid

8 rows selected.

SQL> exec :TODAY := '2011-10-02'

PL/SQL procedure successfully completed.

SQL> select tid
  2       , tname
  3       , effectivedate
  4       , case
  5         when  to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
  6           and to_date(:TODAY,'yyyy-mm-dd') < next_effectivedate
  7         then
  8           'Valid'
  9         when to_date(:TODAY,'yyyy-mm-dd') >= effectivedate
 10         then
 11           'Invalid'
 12         else
 13           'Inactive'
 14         end status
 15    from ( select tid
 16                , tname
 17                , effectivedate
 18                , lead(effectivedate,1,date '9999-12-31') over (partition by tname order by effectivedate) next_effectivedate
 19             from mytable
 20         )
 21  /

       TID T EFFECTIVEDATE       STATUS
---------- - ------------------- --------
         1 A 01-07-2011 00:00:00 Invalid
         2 A 01-08-2011 00:00:00 Invalid
         3 A 01-09-2011 00:00:00 Invalid
         4 A 01-10-2011 00:00:00 Valid
         5 B 01-08-2011 00:00:00 Invalid
         6 B 01-09-2011 00:00:00 Invalid
         7 B 01-10-2011 00:00:00 Valid
         8 C 01-09-2011 00:00:00 Valid

8 rows selected.

Saludos,
Rob.