John Doe
John Doe

Reputation: 71

How to make two conditions within a DECODE

I am making a report and for this report I need to ensure that two conditions are met within one DECODE in Oracle, I have recently known the function DECODE and that is why my question arises.

What I have made of me DECODE is the following:

DECODE (CODE, 'PR', 'Number 1',
              'ES', 'Number 2',

For my two conditions to be fulfilled I need him to CODE be 'PO' and also that his EQUIV be equal to 85. These would be the two conditions that I need to be met within DECODE, in addition to what I already have working.

Searching in the official Oracle documentation I did not find something similar to what I require Documentation

Upvotes: 1

Views: 8099

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270401

Don't use decode(). It is bespoke Oracle syntax. Since time immemorial (almost), SQL has supported case expressions -- which are more flexible than decode() and available in all (real) databases.

So, for your code snippet:

(CASE WHEN CODE = 'PR'
      THEN 'El Título de '
      WHEN CODE = 'ES'
      THEN 'El Diploma de '
      WHEN CODE = 'PO' AND EQUIV = 85
      THEN 'Jest absolwentem '
 END)

You can add any conditions you want to the WHEN -- including subqueries if you need.

Upvotes: 4

Related Questions