Question : Oracle Decode

Can i use decode to do something like the following:

i want to create a column say content  that is based on a value in column paramater

if paramater = "report" then i want to use the value in the report column
if paramater = "exhibit" then i want to use the value in the exhibit column

All the columns are in the same table, but it's teh paramater that determines which column = "content"

does this make sense? If not, i can clarify. I was thinking of using the decode statement, just as in SQL Server i would use the Case/When Statement. Can this be done in oracle, without having to write a cursor ?

Thanks for your help in advance.

b

Answer : Oracle Decode

oracle states that case-when-statements are faster than decode. also, when using case-when you are ANSI compliant an can reuse the script if you want.

I have attached sample code that might help you.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
-- drop table if exists
DROP TABLE your_table;

-- create sample table
CREATE TABLE your_table
(
   report         VARCHAR2 ( 10 )
 , exhibit        VARCHAR2 ( 10 )
 , param          VARCHAR2 ( 10 )
);


-- insert test values
INSERT INTO your_table ( report
                       , param )
    VALUES ( 'myreport'
           , 'report' );

INSERT INTO your_table ( exhibit
                       , param )
    VALUES ( 'myexhibit'
           , 'exhibit' );
COMMIT;

-- show table
SELECT *
  FROM your_table;


-- add content column
ALTER TABLE your_table ADD content varchar2(100);

-- update content column
UPDATE your_table
   SET content      =
          CASE param
             WHEN 'report' THEN report
             WHEN 'exhibit' THEN exhibit
             ELSE NULL
          END;

COMMIT;


-- show the new table
SELECT *
  FROM your_table;
Random Solutions  
 
programming4us programming4us