“无效月份” -Oracle

时间:2019-02-21 15:53:50

标签: sql oracle

CREATE TABLE MRE_BUDGET as (
   select OUTBOUND, INBOUND, vehicle_size, rental_date as start_date, TO_CHAR(TO_DATE((LEAD(Rental_date) over (partition by OUTBOUND, INBOUND, vehicle_size order by OUTBOUND, INBOUND, vehicle_size, rental_date)), 'YYYYMMDD')-1, 'YYYYMMDD') as end_date, Rate  
   from (select OUTBOUND, INBOUND, vehicle_size, rental_date, Avg(RATE) as RATE from mre_competitor_data1 where competitor='BUDGET' group by OUTBOUND, INBOUND, vehicle_size, rental_date)
   where rental_date <= (select cpd from mre_system_parameters));

,错误提示“ 无效月份” ...我需要删除没有有效月份的日期?不知道该怎么做??

1 个答案:

答案 0 :(得分:2)

  

我需要删除没有有效月份的日期?

您的源表数据没有有效月份中的日期-使用正确的数据类型时(几乎)是不可能的。错误来自隐式转换,该隐式转换以一种格式创建字符串,然后是使用另一种格式的显式转换返回日期。

您当前正在做

 TO_CHAR(
   TO_DATE(
     (LEAD(Rental_date) over (partition by OUTBOUND, INBOUND, vehicle_size
        order by OUTBOUND, INBOUND, vehicle_size, rental_date)),
     'YYYYMMDD'
   )-1,
   'YYYYMMDD'
 ) as end_date,

rental_date是(希望)是一个日期列,将其传递给lead()而不管to_date()是什么,

 TO_CHAR(
   TO_DATE(
     TO_CHAR(
       LEAD(Rental_date) over (partition by OUTBOUND, INBOUND, vehicle_size
         order by OUTBOUND, INBOUND, vehicle_size, rental_date)m
       <your_current_NLS_DATE_FORMAT>,
      ),
     'YYYYMMDD'
   )-1,
   'YYYYMMDD'
 ) as end_date,

,而您的NLS_DATE_FORMAT与以后使用的格式不匹配。作为一个固定日期的示例:

select
  TO_CHAR(
    TO_DATE(
      TO_CHAR(
        date '2019-02-28',
        'DD-MM-YYYY' -- guessing your NLS_DATE_FORMAT
      ),
      'YYYYMMDD'
     ) -1,
    'YYYYMMDD'
  )
from dual;

ORA-01843: not a valid month

您可能只是错误地使用了TO_CHARTO_DATE

select
  TO_DATE(
    TO_CHAR(
      date '2019-02-28',
      'YYYYMMDD'
     ) -1,
    'YYYYMMDD'
  )
from dual;

TO_DATE(TO
----------
2019-02-27

但是我不确定为什么您可以做到的时候才转换值:

select date '2019-02-28' - 1
from dual;

DATE'2019-
----------
2019-02-27

或您的情况:

LEAD(Rental_date) over (partition by OUTBOUND, INBOUND, vehicle_size
  order by OUTBOUND, INBOUND, vehicle_size, rental_date) - 1 as end_date

原位,去掉多余的括号,可能像这样:

CREATE TABLE MRE_BUDGET as
select OUTBOUND, INBOUND, vehicle_size, rental_date as start_date,
  LEAD(Rental_date) over (partition by OUTBOUND, INBOUND, vehicle_size
    order by OUTBOUND, INBOUND, vehicle_size, rental_date) - 1 as end_date,
  Rate  
from (
  select OUTBOUND, INBOUND, vehicle_size, rental_date, Avg(RATE) as RATE
  from mre_competitor_data1
  where competitor='BUDGET'
  group by OUTBOUND, INBOUND, vehicle_size, rental_date
)
where rental_date <= (select cpd from mre_system_parameters));