Using IF ELSE in Oracle Using IF ELSE in Oracle sql sql

Using IF ELSE in Oracle


IF is a PL/SQL construct. If you are executing a query, you are using SQL not PL/SQL.

In SQL, you can use a CASE statement in the query itself

SELECT DISTINCT a.item,                 (CASE WHEN b.salesman = 'VIKKIE'                      THEN 'ICKY'                      ELSE b.salesman                  END),                 NVL(a.manufacturer,'Not Set') Manufacturer  FROM inv_items a,        arv_sales b WHERE  a.co = '100'   AND a.co = b.co   AND A.ITEM_KEY = b.item_key      AND a.item LIKE 'BX%'   AND b.salesman in ('01','15')   AND trans_date BETWEEN to_date('010113','mmddrr')                      and to_date('011713','mmddrr')ORDER BY a.item

Since you aren't doing any aggregation, you don't want a GROUP BY in your query. Are you really sure that you need the DISTINCT? People often throw that in haphazardly or add it when they are missing a join condition rather than considering whether it is really necessary to do the extra work to identify and remove duplicates.


You can use Decode as well:

SELECT DISTINCT a.item, decode(b.salesman,'VIKKIE','ICKY',Else),NVL(a.manufacturer,'Not Set')ManufacturerFROM inv_items a, arv_sales bWHERE a.co = b.co      AND A.ITEM_KEY = b.item_key      AND a.co = '100'AND a.item LIKE 'BX%'AND b.salesman in ('01','15')AND trans_date BETWEEN to_date('010113','mmddrr')                         and to_date('011713','mmddrr')GROUP BY a.item, b.salesman, a.manufacturerORDER BY a.item