Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.tools -> Re: DECODE question
In article <9i4vcp$u2o$1_at_newsg4.svr.pol.co.uk>, "Jason" says...
>
>Having been used to Access and SQLServer, I am hitting a couple of problems
>translating some SQL code to Oracle. Here's one that I can't seem to work
>out.
>
>Consider the following Access code. Basically, it looks at a field called
>RotaShift. If this is filled in, that is the field that is used, otherwise
>the field AltShift is used.
>
>IIf ( RotaShift IS NULL, AltShift, RotaShift) AS Shift
>
>Now in trying to translate this to Oracle, I have come up with :-
>
>DECODE (RotaShift, Null, AltShift, RotaShift) AS Shift
>
>However, this doesn't seem to work. From what I can see, the 3rd and 4th
>arguments can only be numeric values, not fields. Is there a workaround for
>this.
>
>
why doesn't it work? getting an error?
Decode can return any type -- if altshift and rotashift are different types you might have to 'to_xxxx' the return type:
scott_at_ORA8I.WORLD> select decode( comm, null, ename, to_char( comm ) ) from emp;
DECODE(COMM,NULL,ENAME,TO_CHAR(COMM))
14 rows selected.
Although, NVL might be easier for you (an "IF NULL THEN" function)
scott_at_ORA8I.WORLD> select nvl(to_char(comm), ename ) from emp;
NVL(TO_CHAR(COMM),ENAME)
14 rows selected.
>TIA
>
>
>Jason.
>
>
-- Thomas Kyte (tkyte@us.oracle.com) http://asktom.oracle.com/ Expert one on one Oracle, programming techniques and solutions for Oracle. http://www.amazon.com/exec/obidos/ASIN/1861004826/ Opinions are mine and do not necessarily reflect those of Oracle CorpReceived on Fri Jul 06 2001 - 15:57:52 CDT