DECODE and substr function : DECODE « Conversion Functions « Oracle PL / SQL






DECODE and substr function

   
SQL>
SQL>
SQL> CREATE TABLE old_item (
  2       item_id   CHAR(20),
  3       item_desc CHAR(25)
  4       );

Table created.

SQL>
SQL> INSERT INTO old_item VALUES('LA-101', 'Can, Small');

1 row created.

SQL> INSERT INTO old_item VALUES('LA-102', 'Can, Large');

1 row created.

SQL> INSERT INTO old_item VALUES('LA-103', 'Bottle, Small');

1 row created.

SQL> INSERT INTO old_item VALUES('LA-104', 'Bottle, Large');

1 row created.

SQL> INSERT INTO old_item VALUES('NY-101', 'Box, Small');

1 row created.

SQL> INSERT INTO old_item VALUES('NY-102', 'Box, Large');

1 row created.

SQL> INSERT INTO old_item VALUES('NY-103', 'Shipping Carton, Small');

1 row created.

SQL> INSERT INTO old_item VALUES('NY-104', 'Shipping Carton, Large');

1 row created.

SQL>
SQL>
SQL> SELECT DECODE(SUBSTR(item_id, 1, 2),
  2                'LA', 'Western',
  3                'NY', 'Eastern',
  4                '* Unknown *'
  5                ) "Region",
  6         SUBSTR(item_id, 4,3) "Item ID",
  7         item_desc
  8  FROM   old_item;

Region      Ite ITEM_DESC
----------- --- -------------------------
Western     101 Can, Small
Western     102 Can, Large
Western     103 Bottle, Small
Western     104 Bottle, Large
Eastern     101 Box, Small
Eastern     102 Box, Large
Eastern     103 Shipping Carton, Small
Eastern     104 Shipping Carton, Large

8 rows selected.

SQL>
SQL> drop table OLD_ITEM;

Table dropped.

SQL>
SQL>
SQL>

   
    
  








Related examples in the same category

1.Syntax: DECODE
2.Decode char value to number
3.Decode statement could be easily rewritten using the CASE statement
4.Decode for more than one key value pair
5.Use Decode as if statement to logic like 'if then else'
6.DECODE and GROUPING
7.Create a report that shows number of customers per state.
8.Use DECODE and SIGN functions on the ORD table
9.Extract any procedure, function or package
10.Decode the result from row_number over, partition by, order by
11.Decode a column with substr and instr
12.Format nullable heading
13.DECODE in the GROUP BY clause
14.Demo range comparison with DECODE
15.Use decode as if statement and output 'high' or 'low'
16.Use decode create dynamic select statement
17.decode column data to output more meaningful information