Oracle SQL:From_date和To_date之间

时间:2017-02-06 22:01:03

标签: sql oracle

我想在日期之间选择 - 日期到 - 我收到错误无效的号码

  SELECT tt.id_str_rt store
        ,SUBSTR(tt.inf_ct,1,5) zip_code
        ,COUNT(tt.ai_trn) tran_count
        ,SUM(tr.mo_nt_tot) sales_value
    FROM orco_owner.tr_trn tt
        ,orco_owner.tr_rtl tr
   WHERE tt.id_str_rt = tr.id_str_rt
     AND tt.id_str_rt = NVL(:PM_store_number,tt.id_str_rt)
     AND NVL(SUBSTR(tt.inf_ct,1,5),0) = NVL(:PM_zip_code,NVL(SUBSTR(tt.inf_ct,1,5),0))
     AND tt.id_ws = tr.id_ws
     AND tt.dc_dy_bsn = tr.dc_dy_bsn
     AND tt.ai_trn = tr.ai_trn
     AND TRUNC(TO_DATE(tt.dc_dy_bsn,'yyyy-MM-dd')) 
 BETWEEN TO_DATE(TRUNC(NVL(:PM_date_from,TO_DATE('1901-01-01','yyyy-MM-dd'))),'yyyy-MM-dd') 
     AND TO_DATE (NVL(:PM_date_to, '2099-12-31'), 'yyyy-mm-dd')
     AND LENGTH(TRIM(TRANSLATE(SUBSTR(inf_ct,1,5), '0123456789', ' '))) IS NULL
GROUP BY tt.id_str_rt,SUBSTR(tt.inf_ct,1,5)
ORDER BY zip_code   ;

2 个答案:

答案 0 :(得分:0)

为什么一切都要两次?试试这个:

TO_DATE(NVL(TRIM(:PM_date_from),'1901-01-01'),'yyyy-MM-dd')

如果值为null,则修剪该值替换为默认值,然后转换为日期。

您收到的错误可能是:PM_date_from格式不正确,或者'yyyy-mm-dd'可能是'yyyy-MM-dd'

答案 1 :(得分:0)

我能够在SQL Developer中使用以下代码,但在BI Publisher 10 G中,我得到“Not a valid month”

 SELECT tt.id_str_rt store
       ,SUBSTR(tt.inf_ct,1,5) zip_code
       ,COUNT(tt.ai_trn) tran_count
       ,SUM(tr.mo_nt_tot) sales_value  
    FROM orco_owner.tr_trn tt
       ,orco_owner.tr_rtl tr
    WHERE tt.id_str_rt = tr.id_str_rt
    AND tt.id_str_rt = NVL(:PM_store_number,tt.id_str_rt)
    AND NVL(SUBSTR(tt.inf_ct,1,5),0) = NVL(:PM_zip_code,NVL(SUBSTR(tt.inf_ct,1,5),0))
    AND tt.id_ws = tr.id_ws
    AND tt.dc_dy_bsn = tr.dc_dy_bsn
    AND tt.ai_trn = tr.ai_trn
    AND TRUNC (TO_DATE(tt.dc_dy_bsn,'yyyy-MM-dd'))
    BETWEEN TO_DATE (NVL(:PM_date_from, '1901/01/01'),'yyyy/MM/dd')
    AND TO_DATE (NVL(:PM_date_to, '2099/01/01'),'yyyy/MM/dd')   
    AND LENGTH(TRIM(TRANSLATE(SUBSTR(inf_ct,1,5), '0123456789', ' '))) IS NULL
    GROUP BY tt.id_str_rt,SUBSTR(tt.inf_ct,1,5)
    ORDER BY zip_code, store ;