Decode a column with substr and instr : DECODE « Conversion Functions « Oracle PL / SQL






Decode a column with substr and instr

   
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> SELECT item_desc,
  2         SUBSTR(item_desc,
  3                INSTR(item_desc,
  4                      ',',
  5                      1
  6                      ) +2,
  7                99
  8                )
  9  FROM   old_item;

ITEM_DESC                 SUBSTR(ITEM_DESC,INSTR(IT
------------------------- -------------------------
Can, Small                Small
Can, Large                Large
Bottle, Small             Small
Bottle, Large             Large
Box, Small                Small
Box, Large                Large
Shipping Carton, Small    Small
Shipping Carton, Large    Large

8 rows selected.

SQL>
SQL> drop table OLD_ITEM;

Table dropped.

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 and substr function
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