Focal Point
[CLOSED] DECODE: Using a field value for default

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/7587021916

April 17, 2012, 03:03 PM
Deborah Sullivan
[CLOSED] DECODE: Using a field value for default
I am hoping this is a stupid question, but I am having trouble finding documentation for it. Maybe someone can easily answer for me:

Here is the scenario: I am using DECODE to reassign some field values. Pretty straight forward.

So,

DECODE FIELD1('The Fieldname Value A' 'A'
'The Field Value B' 'B' ...


That sort of thing. But instead of assigning a default literal value, like: ELSE 'Other', I want to use the incoming field value if that value has not been included in the DECODE.

So, instead of:

DECODE FIELD1('The Fieldname Value A' 'A'
                    'The Field Value B' 'B'
                    ELSE 'Other');



I want something like:

DECODE FIELD1('The Fieldname Value A' 'A'
                    'The Field Value B' 'B'
                    ELSE FIELD1);




Where the table value of FIELD1 is displayed, if a decoded value has not been supplied for that field value.

I am sure that I had some method of doing this at some point (and that it was simple), but I'm banging my head on the desk a bit trying to figure it out now. I am sure that I could do it with a couple of DEFINE fields, but I am trying to avoid getting into a large, convoluted IF THEN ELSE statement.

Any tips would be appreciated.

This message has been edited. Last edited by: Kerry,
April 17, 2012, 04:23 PM
Prarie
You could try something like this.

DEFINE FILE CAR
NEWCAR/A20 =   DECODE CAR ('JAGUAR' 'JAG' 'JENSEN' 'JEN' );
NEWCAR2/A20 = IF NEWCAR NE ' ' THEN NEWCAR ELSE CAR;
END
TABLE FILE CAR
PRINT CAR NEWCAR NEWCAR2
END
  



In Focus since 1993. WebFOCUS 7.7.03 Win 2003
@Prairie

Maybe it works as is, but I always make the default of a DECODE assignment explicit:

... = DECODE CAR ('JAGUAR' 'JAG', 'JENSEN' 'JEN', ELSE ' ');
Jack,

Do you always put commas in your DECODE?
I tested it and IT WORKS although never mentioned in the documentation!
Another WF wonder?


Daniel
In Focus since 1982
wf 8.202M/Win10/IIS/SSA - WrapApp Front End for WF

The bottom of the function definition in the documentation says:
quote:
You can use up to 40 lines to define the code and result pairs for any given DECODE function, or 39 lines if you also use an ELSE phrase. Use either a comma or blank to separate the code from the result, or one pair from another.


So in practice you can put those comma's wherever you like and it should work.

So apparently you could also write it as:
... = DECODE CAR ('JAGUAR','JAG' 'JENSEN','JEN' ELSE '');
... = DECODE CAR ('JAGUAR','JAG','JENSEN','JEN',ELSE '');


I am a bit surprised that a comma before the ELSE statement is accepted though!


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
is mentioned

quote:
You can use up to 40 lines to define the code and result pairs for any given DECODE function,
or 39 lines if you also use an ELSE phrase. Use either a comma or blank to separate the
code from the result, or one pair from another.


Wep5622 just beat me
j.gross - yes the ELSE ' ' should be added.

Interesting about the comma...