[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 870
  • Last Modified:

Problem with decode in the where clause

I have a problem with the following query:

SELECT
      TOL.product_heading,
      TOL.tenor_category,
      TOL.tolerance_pct,
      RE.product_heading_order,
      decode(TOL.tenor_category,'SHORT',1000000,'MED',2000000,'LONG',3000000) AS FROM_TENOR,
      RE.trade_id,
      RE.dealer,
      RE.profit_centre,
      RE.counterparty_code,
      TO_CHAR(DMO_FUNCTIONS.TO_ORACLE_DATE(value_date,'YYYYMMDD'),'DD-Mon-YYYY') AS      VALUE_DATE,
      RE.instrument,
      RE.instrument_description,
      RE.rate,
      TEMP.avg_rate,
      'Tolerances used in this report' AS TOLERANCE_TEXT    
FROM  
      dmo_days_prodheading_rate RE,
      dmo_rate_tolerance_lookup TOL,
      (SELECT
             AVG(RE.rate) AS AVG_RATE,
             RE.product_heading,
             DECODE (GREATEST (2555, daysbucket), 2555,'SHORT',
             DECODE (GREATEST (5475, daysbucket), 5475,'MED','LONG')) AS TENOR_CATEGORY  
       FROM
             dmo_days_prodheading_rate RE
       WHERE
             RE.product_heading_order >19
       GROUP BY
             RE.product_heading,
             DECODE (GREATEST (2555, daysbucket), 2555,'SHORT',
             DECODE (GREATEST (5475, daysbucket), 5475,'MED','LONG'))    
      )TEMP                        
WHERE
      TEMP.product_heading = TOL.product_heading AND
      TEMP.tenor_category  = TOL.tenor_category AND
      TOL.product_heading  = RE.product_heading AND
      TOL.tenor_category   = DECODE (GREATEST (2555, daysbucket), 2555,'SHORT',
                             DECODE (GREATEST (5475, daysbucket), 5475,'MED','LONG')) AND
      product_heading_order >19 AND
      rate NOT BETWEEN
      (AVG_RATE - (AVG_RATE*TOL.Tolerance_pct/100)) AND
      (AVG_RATE + (AVG_RATE*TOL.Tolerance_pct/100))    

 I get the following error:

ORA - 01843: Not a valid month

 The problem appears to lie with the
TOL.tenor_category   = DECODE (GREATEST (2555, daysbucket), 2555,'SHORT',
                                  DECODE (GREATEST (5475, daysbucket), 5475,'MED','LONG'))

line as commenting this out allows the query to run.

Why do I get the 01843 error when this line is included? The daysbucket field is a number generated by subtracting 2 dates. The TOL.tenor_category field is a varchar2(20) field.

Thanks,

Rich.

0
RichardKnight
Asked:
RichardKnight
  • 4
  • 3
  • 3
  • +3
1 Solution
 
seazodiacCommented:
How do you generate daysbucket?

the reason why you get this error is one of the 2 dates you are using to derive daysbucket does not have a valid date format
0
 
bvanderveenCommented:
Looks like this error is from:
>> TO_CHAR(DMO_FUNCTIONS.TO_ORACLE_DATE(value_date,'YYYYMMDD'),'DD-Mon-YYYY') AS      VALUE_DATE,

Don't know what the function you are calling is, but "not a valid month" is from a character-to-date conversion error.  Look at what is being generated by:
    DMO_FUNCTIONS.TO_ORACLE_DATE(value_date,'YYYYMMDD')
and see if it is an Oracle date.
0
 
andrewstCommented:
My money would be on this line:

TO_CHAR(DMO_FUNCTIONS.TO_ORACLE_DATE(value_date,'YYYYMMDD'),'DD-Mon-YYYY') AS      VALUE_DATE,

What does DMO_FUNCTIONS.TO_ORACLE_DATE look like?  If it returns a CHAR value like '20040825' then the line above is equivalent to:

TO_CHAR(TO_CHAR(value_date,'YYYYMMDD'),'DD-Mon-YYYY') AS      VALUE_DATE,

... which doesn't make sense.

OTOH, if it takes a char parameter and returns a DATE then is value_date a character string in the required 'YYYYMMDD' format?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
bvanderveenCommented:
Seazodiac may be right.  I was assuming that daysbucket was stored as an integer value, not derived.  If it is derived, you have a problem with the date conversions in this computation, as he said.
0
 
RichardKnightAuthor Commented:
The value date was the first place I looked as it was the obvious place to try. Unfortunately commenting this out doesn't change things. The whole query runs correctly until you put in the decode join:

 TOL.tenor_category   = DECODE (GREATEST (2555, daysbucket), 2555,'SHORT',
                                   DECODE (GREATEST (5475, daysbucket), 5475,'MED','LONG'))

and I can't see why this gives a problem.
0
 
seazodiacCommented:
Rich, as I said in the very first post, how do you derive daysbucket? that's the key, which most often is ignored by people..
0
 
RichardKnightAuthor Commented:
Daysbucket comes from the dmo_days_prodheading_rate view:


decode (FU.product_type, 'RPO',TO_DATE (FU.effective_end_date,'YYYYMMDD') -
                                      TO_DATE (FU.effective_start_date,'YYYYMMDD'),
                                      TO_DATE (FU.expir_matur_century,'YYYYMMDD') -  
                                                                            decode(least(FU.deliv_century,  expir_matur_century),
                                                   expir_matur_century, TO_DATE(expir_matur_century,'YYYYMMDD'),
                                                   TO_DATE(FU.deliv_century,'YYYYMMDD'))) AS DAYSBUCKET,

The code used to generate the view works fine when run as a standalone query.
0
 
bvanderveenCommented:
>>The code used to generate the view works fine when run as a standalone query.

Try scrolling through all the records in the view.  I'll bet you get an error.  But you won't see it until you scroll through, and the bad formatting is encountered.
0
 
seazodiacCommented:
is FU a table or view?

can you try this and post back if you have any errors?

select TO_DATE (FU.effective_end_date,'YYYYMMDD'),
TO_DATE (FU.effective_start_date,'YYYYMMDD'),
TO_DATE (FU.expir_matur_century,'YYYYMMDD'),
TO_DATE(FU.deliv_century,'YYYYMMDD')
from FU;  --you need to replace FU with the real table or view name here.


0
 
kamar_kCommented:
Try to use the function TO_NUMBER(daysbucket) instead of daysbucket.
0
 
RichardKnightAuthor Commented:
seazodiac - your suggested code does produce an error but I knew it would. There are various checks and clauses in the where clause (I didn't show the where clause when showing my daysbucket code)that prevent any dates getting through that may cause an error. Daysbucket is included in a view that runs perfectly and all of the daysbucket values are as expected with no errors.
0
 
RichardKnightAuthor Commented:
It turns out that the problem was related to the way that Oracle 'optimises' queries.
I was using views and inline views and the predicates were getting mixed up when Oracle was combining the code into one uber-query. A simple 'order by' in the offending view has fixed the problem. If there is a better way of dealing with this not using order by (or rownum or by aggregating) I'd be grateful to hear it. Oracle suggest using decode around any columns that may cause problems and setting the offending values in those columns to NULL. This was too fiddly for my needs - I chose the easy route!
0
 
moduloCommented:
PAQed with points refunded (125)

modulo
Community Support Moderator
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now