CASE .. WHEN expression in Oracle SQL

前端 未结 9 865
北荒
北荒 2020-12-04 13:17

I have the table with 1 column and has following data

Status
a1
i
t
a2
a3

I want to display the following result in my select query

相关标签:
9条回答
  • 2020-12-04 13:36

    Of course...

    select case substr(status,1,1) -- you're only interested in the first character.
                when 'a' then 'Active'
                when 'i' then 'Inactive'
                when 't' then 'Terminated'
           end as statustext
      from stage.tst
    

    However, there's a few worrying things about this schema. Firstly if you have a column that means something, appending a number onto the end it not necessarily the best way to go. Also, depending on the number of status' you have you might want to consider turning this column into a foreign key to a separate table.


    Based on your comment you definitely want to turn this into a foreign key. For instance

    create table statuses ( -- Not a good table name :-)
        status varchar2(10)
      , description varchar2(10)
      , constraint pk_statuses primary key (status)
        )
    
    create table tst (
        id number
      , status varchar2(10)
      , constraint pk_tst primary key (id)
      , constraint fk_tst foreign key (status) references statuses (status)
        )
    

    Your query then becomes

    select a.status, b.description
      from tst a
      left outer join statuses b
        on a.status = b.status
    

    Here's a SQL Fiddle to demonstrate.

    0 讨论(0)
  • 2020-12-04 13:37
    CASE TO_CHAR(META.RHCONTRATOSFOLHA.CONTRATO)
    WHEN '91' AND TO_CHAR(META.RHCONTRATOSFOLHA.UNIDADE) = '0001' THEN '91RJ'
    WHEN '91' AND TO_CHAR(META.RHCONTRATOSFOLHA.UNIDADE) = '0002' THEN '91SP'
    END CONTRATO,
    
    00905. 00000 -  "missing keyword"
    *Cause:    
    *Action:
    Erro na linha: 15 Coluna: 11
    
    0 讨论(0)
  • 2020-12-04 13:43
    SELECT
      STATUS,
      CASE
        WHEN STATUS IN('a1','a2','a3') 
         THEN 'Active'
        WHEN STATUS = 'i' 
         THEN 'Inactive'
        WHEN STATUS = 't'
         THEN 'Terminated'  ELSE null
      END AS STATUSTEXT
    FROM
      stage.tst;
    
    0 讨论(0)
  • 2020-12-04 13:51

    You can rewrite it to use the ELSE condition of a CASE:

    SELECT status,
           CASE status
             WHEN 'i' THEN 'Inactive'
             WHEN 't' THEN 'Terminated'
             ELSE 'Active'
           END AS StatusText
    FROM   stage.tst 
    
    0 讨论(0)
  • 2020-12-04 13:56

    You can only check the first character of the status. For this you use substring function.

    substr(status, 1,1)

    In your case past.

    0 讨论(0)
  • 2020-12-04 13:58

    Since web search for Oracle case tops to that link, I add here for case statement, though not answer to the question asked about case expression:

    CASE
       WHEN grade = 'A' THEN dbms_output.put_line('Excellent');
       WHEN grade = 'B' THEN dbms_output.put_line('Very Good');
       WHEN grade = 'C' THEN dbms_output.put_line('Good');
       WHEN grade = 'D' THEN dbms_output.put_line('Fair');
       WHEN grade = 'F' THEN dbms_output.put_line('Poor');
       ELSE dbms_output.put_line('No such grade');
    END CASE;
    

    or other variant:

    CASE grade
       WHEN 'A' THEN dbms_output.put_line('Excellent');
       WHEN 'B' THEN dbms_output.put_line('Very Good');
       WHEN 'C' THEN dbms_output.put_line('Good');
       WHEN 'D' THEN dbms_output.put_line('Fair');
       WHEN 'F' THEN dbms_output.put_line('Poor');
       ELSE dbms_output.put_line('No such grade');
    END CASE;
    

    Per Oracle docs: https://docs.oracle.com/cd/B10501_01/appdev.920/a96624/04_struc.htm

    0 讨论(0)
提交回复
热议问题