Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: help with decode

Re: help with decode

From: Frank van Bortel <frank.van.bortel_at_gmail.com>
Date: Wed, 03 Aug 2005 21:12:09 +0200
Message-ID: <dcr4hq$cde$1@news3.zwoll1.ov.home.nl>


soup_or_power_at_yahoo.com wrote:
> Can anyone tell me how the ||'s are interpreted in the DECODE below?
> I was thinking string concatenation, but shouldn't there be
> commas after 'M' and 'T'?
> Many thanks for your help.
>
> SELECT
> DECODE(NVL(template_id,0),0,'M' || mailing_id,'T' ||
> template_id),
> from A
>

You owe me a cup of coffee.
I assume there where more lines of code, as the final comma is out of the question.

  1. you have a template_id, which can be NULL.
  2. In order to circumvent NULLs for template_id, there's this NVL(); causing the template_id to have a value always (NULL will become zero (0), because of this.
  3. Keeping the above in mind, you code is: DECODE(template_id,0,'M' || mailing_id,'T' || template_id) from A; 3a: Let's take some values: template_id is 13, mailing_id is 9999: DECODE(13,0,'M' || 9999,'T' || 13) from A; Here's the first bad habit: implicit conversions: you compare/concatenate values to strings. At least in one case: when mailing_id IS NULL (it becomes a numeric zero, not a string '0').
  4. Do the math: Compare 13 to 0, if true, evaluate to 'M', concatenated with 9999. In all other cases, evaluate to 'T' concatenated with 13. In the example above, it would result in 'T13'. 4b: If mailing_id IS NULL, then the equation is: Compare 0 to 0, if true, evaluate to 'M', concatenated with 9999. In all other cases, evaluate to 'T' concatenated with 13. You do the math :)
-- 
Regards,
Frank van Bortel
Received on Wed Aug 03 2005 - 14:12:09 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US