简体   繁体   中英

How can I use case statement in computed column in Oracle database

Can I use Case statement in computed column in Oracle.

I have filename column in table which can be used to populate source of data. eg. if filename contains abc than source system is ABC, if filename contains def then source of data is DEF.

I need to alter the table with new column source (which should be computed from existing file name column)

Yes, you can do that. For example:

CREATE TABLE TEST_TAB
  (N1     NUMBER,
   S2     VARCHAR2(10) GENERATED ALWAYS AS (CASE
                                              WHEN N1 IS NULL THEN 'NULL'
                                              WHEN N1 < 0 THEN 'NEGATIVE'
                                              WHEN N1 = 0 THEN 'ZERO'
                                              WHEN N1 > 0 THEN 'POSITIVE'
                                            END));

dbfiddle here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM