根据某些日期条件生成输出

时间:2016-02-12 06:44:26

标签: sql oracle

我有一张这样的表

START_DATE      END_DATE      Names

01-FEB-16       03-FEB-16      AAA
01-FEB-16       02-FEB-16      BBB
10-FEB-16       11-FEB-16      CCC

如果输入日期是02-Feb-16。如果输入介于Start_date和End_date之间,我想生成所有名称。

(输出应该如下)

Output(For the problem statement above)

Names 
AAA
BBB

2 个答案:

答案 0 :(得分:2)

您只需使用BETWEEN并检查您的日期是否属于这对日期之一。

WITH your_table AS (
  SELECT TO_DATE('01-FEB-16','DD-MON-RR') start_date, TO_DATE('03-FEB-16','DD-MON-RR') end_date, 'AAA' names FROM DUAL
  UNION
  SELECT TO_DATE('01-FEB-16','DD-MON-RR') start_date, TO_DATE('02-FEB-16','DD-MON-RR') end_date, 'BBB' names FROM DUAL
  UNION
  SELECT TO_DATE('10-FEB-16','DD-MON-RR') start_date, TO_DATE('11-FEB-16','DD-MON-RR') end_date, 'CCC' names FROM DUAL
)  
SELECT names
FROM your_table
WHERE TO_DATE('02-FEB-16','DD-MON-RR') BETWEEN start_date AND end_date    

答案 1 :(得分:2)

您好,您可以在

之间使用

e.g。

truncate table DROPME_DATES_NAMES;

drop table DROPME_DATES_NAMES;


create table DROPME_DATES_NAMES (date_from date, date_to date, atr_name varchar2(30));

insert into DROPME_DATES_NAMES values (to_date('01-FEB-16','DD-MON-YYYY'), to_date('03-FEB-16','DD-MON-YYYY'), 'AAA ');
insert into DROPME_DATES_NAMES values (to_date('01-FEB-16','DD-MON-YYYY'),to_date('02-FEB-16','DD-MON-YYYY'),'BBB');
insert into DROPME_DATES_NAMES values (to_date('10-FEB-16','DD-MON-YYYY'),to_date('11-FEB-16','DD-MON-YYYY'),'CCC');


commit;


select atr_name
  from DROPME_DATES_NAMES
where to_date('02-Feb-16','DD-MON-YYYY') between date_from and date_to;