IF condition within an Oracle SELECT [duplicate]

1

There are other questions in this context, but I am lazy on the subject and I do not understand. With this, I need help.

I have SELECT for searching data in a ORACLE database, however I need to make a IF condition within this SELECT .

Example:

I have the following SELECT :

SELECT
CAMPO1, CAMPO2, CAMPO3, CAMPO4, CAMPO5
FROM
TABELA1
WHERE
CAMPO1 = dado1 AND CAMPO2 = dado2

The condition would look like this:

IF CAMPO1 IS NULL, CAMPO1 = CAMPO3

Is it possible to do this condition within this SELECT ? I have researched this, found some examples, but I could not understand.

If anyone can help me.

    
asked by anonymous 28.02.2017 / 22:25

1 answer

2

In the select you can use CASE Ex:

SELECT
     CASE WHEN CAMPO1 IS NULL THEN 'CAMPO1 NULL' 
       ELSE CAMPO1 END AS CAMPO1, 
     CAMPO2, 
     CAMPO3, 
     CAMPO4, 
     CAMPO5
FROM TABELA1
WHERE CAMPO1 = dado1 AND CAMPO2 = dado2

If I understand the question correctly, I think it's more or less what you want.

Reference: CASE ORACLE

And there is also COALESCE, this will return the first non-zero data of the set passed as Ex parameter:

SELECT
     COALESCE(CAMPO1,'CAMPO1 NULL') AS CAMPO1, 
     CAMPO2, 
     CAMPO3, 
     CAMPO4, 
     CAMPO5
 FROM TABELA1
 WHERE CAMPO1 = dado1 AND CAMPO2 = dado2

Reference COALESCE ORACLE

    
01.03.2017 / 23:55