CASE .. WHEN expression in Oracle SQL

前端 未结 9 866
北荒
北荒 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:59

    You could use an IN clause

    Something like

    SELECT
      status,
      CASE
        WHEN STATUS IN('a1','a2','a3')
        THEN 'Active'
        WHEN STATUS = 'i'
        THEN 'Inactive'
        WHEN STATUS = 't'
        THEN 'Terminated'
      END AS STATUSTEXT
    FROM
      STATUS
    

    Have a look at this demo

    SQL Fiddle DEMO

    0 讨论(0)
  • 2020-12-04 14:01

    Following syntax would work :

    ....
    where x.p_NBR =to_number(substr(y.k_str,11,5))
    and x.q_nbr = 
     (case 
     when instr(substr(y.m_str,11,9),'_') = 6   then  to_number(substr(y.m_str,11,5))
     when instr(substr(y.m_str,11,9),'_') = 0   then  to_number(substr(y.m_str,11,9))
      else 
           1
      end
    )
    
    0 讨论(0)
  • 2020-12-04 14:03

    It will be easier to do using decode.

    SELECT
      status,
        decode ( status, 'a1','Active',
                         'a2','Active',
                         'a3','Active',
                         'i','Inactive',
                         't','Terminated',
                         'Default')STATUSTEXT
    FROM STATUS
    
    0 讨论(0)
提交回复
热议问题