Oracle
 sql >> база данни >  >> RDS >> Oracle

Как да получа текущата ефективна дата в Oracle?

Не опитвайте, а вижте сами :-)
И само с достъп до една маса.

Вашата маса:

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.

И вашите два тестови случая:

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.

Поздрави,
Роб.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. ORA-06550:Грешка с грешен номер или тип аргументи при извикване на функция в пакет oracle в ASP.NET

  2. Опции за форматиране на SQLcl (Oracle)

  3. Как да форматирате месеца с римски цифри в Oracle

  4. Определяне дали данните за полето в Oracle са от тип число

  5. java.sql.SQLException:невалидни аргументи в повикването