0
select contract.contract_id
from swcd.scd_contract contract 

left join SCD.COMPANY_TIMELINE_VIEW customer on (customer.company_id=contract.customer_company_id and to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') >= (CASE WHEN customer.EVENT_START_DATE is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else 
trunc(customer.EVENT_START_DATE) END) AND to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') <= (CASE WHEN customer.EVENT_END_DATE is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else 
trunc(customer.EVENT_END_DATE) END)) 

left join SCD.COMPANY_TIMELINE_VIEW vendor on (vendor.company_id=contract.vendor_company_id and to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') >= (CASE WHEN vendor.EVENT_START_DATE is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else
trunc(vendor.EVENT_START_DATE) END) AND to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') <= (CASE WHEN vendor.EVENT_END_DATE is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else 
trunc(vendor.EVENT_END_DATE) END));

这是我在 Oracle 10g 上执行的查询。这对我来说是一种奇怪的行为。当我使用任何一个联接执行select查询时,它都可以正常工作,但是对于所需的联接,它都说ora-01861 literal does not match format string.

-- Works fine
select contract.contract_id
from swcd.scd_contract contract 

left join SCD.COMPANY_TIMELINE_VIEW customer on (customer.company_id=contract.customer_company_id and to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') >= (CASE WHEN customer.EVENT_START_DATE is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else 
trunc(customer.EVENT_START_DATE) END) AND to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') <= (CASE WHEN customer.EVENT_END_DATE is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else 
trunc(customer.EVENT_END_DATE) END));

-- Works fine
select contract.contract_id
from swcd.scd_contract contract 
left join SCD.COMPANY_TIMELINE_VIEW vendor on (vendor.company_id=contract.vendor_company_id and to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') >= (CASE WHEN vendor.EVENT_START_DATE is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else
trunc(vendor.EVENT_START_DATE) END) AND to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '1-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '1-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '1-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '1-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') <= (CASE WHEN vendor.EVENT_END_DATE is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else 
trunc(vendor.EVENT_END_DATE) END));

请帮助我,任何答案将不胜感激。

Error report:
SQL Error: ORA-01861: literal does not match format string
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.
4

2 回答 2

1

这似乎是数据库中的一个错误。无论如何,尝试将 '1-JUL-' 替换为 '01-JUL-' 等等。您的选择在 ELSE 子句中使用隐式格式转换 to_char(contract.ANNOUNCEMENT_DATE),这也可能是一个问题。

我会重写选择以尽可能避免转换,就像这样

select contract.contract_id
from 
  (select contract_id, customer_company_id, vendor_company_id,
          nvl(ANNOUNCEMENT_DATE, 
              add_months(trunc(to_date(ANNOUNCEMENT_YEAR,'yyyy'),'yyyy'),
                 case when contract.ANNOUNCEMENT_QUARTER='Q1' then 0
                      when contract.ANNOUNCEMENT_QUARTER='Q1' then 3
                      when contract.ANNOUNCEMENT_QUARTER='Q1' then 6
                      when contract.ANNOUNCEMENT_QUARTER='Q1' then 9
                 end)) quarter_start
     from swcd.scd_contract) contract
left join SCD.COMPANY_TIMELINE_VIEW customer on
  (customer.company_id=contract.customer_company_id and
   contract.quarter_start between  nvl(trunc(customer.EVENT_START_DATE), to_date('01-JAN-1901', 'DD-Mon-YYYY')) AND
                                   nvl(trunc(customer.EVENT_END_DATE, to_date('01-JAN-2101', 'DD-Mon-YYYY')))) 
left join SCD.COMPANY_TIMELINE_VIEW vendor on
  (vendor.company_id=contract.vendor_company_id and
   contract.quarter_start between  nvl(trunc(vendor.EVENT_START_DATE), to_date('01-JAN-1901', 'DD-Mon-YYYY')) AND
                                   nvl(trunc(vendor.EVENT_END_DATE, to_date('01-JAN-2101', 'DD-Mon-YYYY'))));
于 2012-10-19T07:33:33.093 回答
0
select contract.contract_id
from swcd.scd_contract contract 

left join SCD.COMPANY_TIMELINE_VIEW customer on (customer.company_id=contract.customer_company_id and to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '01-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '01-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '01-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '01-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') >= (CASE WHEN customer.EVENT_START_DATE is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else 
trunc(customer.EVENT_START_DATE) END) AND to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '01-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '01-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '01-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '01-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') <= (CASE WHEN customer.EVENT_END_DATE is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else 
trunc(customer.EVENT_END_DATE) END) 
and contract.customer_company_id is not null)

left join SCD.COMPANY_TIMELINE_VIEW vendor on (vendor.company_id=contract.vendor_company_id and to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '01-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '01-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '01-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '01-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') >= (CASE WHEN vendor.EVENT_START_DATE is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else
trunc(vendor.EVENT_START_DATE) END) AND to_char(to_date(case when contract.ANNOUNCEMENT_DATE is null then case when contract.ANNOUNCEMENT_QUARTER='Q1' then '01-JAN-' when contract.ANNOUNCEMENT_QUARTER='Q2' then '01-APR-'
when contract.ANNOUNCEMENT_QUARTER='Q3' then '01-JUL-' when contract.ANNOUNCEMENT_QUARTER='Q4' then '01-OCT-' end || contract.ANNOUNCEMENT_YEAR else to_char(contract.ANNOUNCEMENT_DATE) end),'dd-MON-YYYY') <= (CASE WHEN vendor.EVENT_END_DATE is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else 
trunc(vendor.EVENT_END_DATE) END) 
and contract.vendor_company_id is not null);

工作正常,id连接子句中的列面临null比较问题。因此添加is not nulljoin子句,现在运行良好。

于 2012-10-19T13:54:08.327 回答