从日期间隔中选择确定天数,通过标识符1,2,3对应于天

时间:2017-06-21 12:54:00

标签: sql oracle

我试图从间隔日期开始。示例:如果我的初始日期为2017年6月21日至2017年7月21日。我想选择周三和周三从该日期间隔开始,我怎样才能在oracle db中创建它?

欣赏帮助!

更新..

CREATE OR REPLACE PROCEDURE "CHAIRA"."PR_SAVEROOMS"
    (v_return OUT SYS_REFCURSOR,INIDATE IN VARCHAR2,ENDDATE IN VARCHAR2,DAYS IN NUMBER) AS
        v_error EXCEPTION;
        v_notificacion t_notificacion;
        -- variable del tipo varray[3] definida asi: [tipoNotificacion: |notificacion|notificacionPin|error|errorPin|advertencia|advertenciaPin|][tituloNotificacion][mensajeNotificacion]
   VARIABLE startDate varchar2;
   VARIABLE endDate varchar2;

BEGIN
:startDate := INIDATE;
:endDate := ENDDATE;

     SELECT day
FROM   ( 
  SELECT DATE TO_DATE(startDate, 'DD/MM/YYYY') + LEVEL - 1 as day
  FROM   DUAL
  CONNECT BY DATE TO_DATE(startDate, 'DD/MM/YYYY') + LEVEL - 1 <= DATE TO_DATE(endDate, 'DD/MM/YYYY')
)
WHERE day - TRUNC( day, 'IW' ) IN ( 2, 4 );

        v_notificacion := t_notificacion('notificacion','Información','Mensaje por defecto');
    OPEN v_return FOR SELECT v_notificacion(1) "_TIPO", v_notificacion(2) "_TITULO",v_notificacion(3)"_MENSAJE" FROM DUAL;
    EXCEPTION
        WHEN v_error THEN
            RAISE_APPLICATION_ERROR(-20333,v_notificacion(1)||'*'||v_notificacion(2)||'*'||v_notificacion(3)||'*');
            ROLLBACK;
 END; 

我根据MT0的建议更新了仍然会导致错误,..

我给DD / MM / YYYY格式化VARCHAR日期......

2 个答案:

答案 0 :(得分:0)

这将获得所有日期:

SELECT DATE '2017-06-21' + LEVEL - 1 as day
FROM   DUAL
CONNECT BY DATE '2017-06-21' + LEVEL - 1 <= DATE '2017-07-21';

然后你只需要在星期三和星期五之前过滤。为此,您可以使用由TRUNC( day, 'IW' )给出的iso周的日期和开始之间的差异,该值始终为星期一(并且与NLS_DATE_LANGUAGENLS_TERRITORY设置无关 - 与使用TO_CHAR查找星期几不同:

SELECT day
FROM   ( 
  SELECT DATE '2017-06-21' + LEVEL - 1 as day
  FROM   DUAL
  CONNECT BY DATE '2017-06-21' + LEVEL - 1 <= DATE '2017-07-21'
)
WHERE day - TRUNC( day, 'IW' ) IN ( 2, 4 );

<强>更新

添加绑定参数:

CREATE OR REPLACE PROCEDURE "CHAIRA"."PR_SAVEROOMS"(
  v_return OUT SYS_REFCURSOR,
  INIDATE IN VARCHAR2,
  ENDDATE IN VARCHAR2,
  DAYS IN NUMBER
)
AS
  v_error                  EXCEPTION;
  v_notificacion           t_notificacion;
  v_wednesdays_and_fridays SYS.ODCIDATELIST;  
BEGIN
  SELECT day
  BULK COLLECT INTO v_wednesdays_and_fridays
  FROM   ( 
    SELECT TO_DATE(iniDate, 'DD/MM/YYYY') + LEVEL - 1 as day
    FROM   DUAL
    CONNECT BY TO_DATE(iniDate, 'DD/MM/YYYY') + LEVEL - 1 <= TO_DATE(endDate, 'DD/MM/YYYY')
  )
  WHERE day - TRUNC( day, 'IW' ) IN ( 2, 4 );

  v_notificacion := t_notificacion('notificacion','Información','Mensaje por defecto');
  OPEN v_return FOR
    SELECT v_notificacion(1) "_TIPO",
           v_notificacion(2) "_TITULO",
           v_notificacion(3) "_MENSAJE"
    FROM DUAL;
EXCEPTION
  WHEN v_error THEN
    RAISE_APPLICATION_ERROR(-20333,v_notificacion(1)||'*'||v_notificacion(2)||'*'||v_notificacion(3)||'*');
    ROLLBACK;
END; 
/

答案 1 :(得分:0)

如果您在不同的日子里运行脚本,我们正在使用 sysdate ,您应该调整一些内容

select * from (SELECT TRUNC (SYSDATE + (ROWNUM*7)) dt
  FROM DUAL 
  CONNECT BY ROWNUM < 100 ) where dt between '21-JUN-17 00:00:00' and '21-JUL-17 00:00:00'
   ;