DECODE( ) function in SQL Server

后端 未结 8 1889
傲寒
傲寒 2020-12-03 04:42
SELECT PC_COMP_CODE,
       \'R\',
       PC_RESUB_REF,
       DECODE(PC_SL_LDGR_CODE, \'02\', \'DR\', \'CR\'),
       PC_DEPT_NO DEPT,
       \'\', --PC_DEPT_NO,
           


        
相关标签:
8条回答
  • 2020-12-03 05:20

    If I understand the question correctly, you want the equivalent of decode but in T-SQL

    Select YourFieldAliasName =
    CASE PC_SL_LDGR_CODE
        WHEN '02' THEN 'DR'
        ELSE 'CR'
    END
    
    0 讨论(0)
  • 2020-12-03 05:22

    In my Case I used it in a lot of places first example if you have 2 values for select statement like gender (Male or Female) then use the following statement:

    SELECT CASE Gender WHEN 'Male' THEN 1 ELSE 2 END AS Gender
    

    If there is more than one condition like nationalities you can use it as the following statement:

    SELECT CASE Nationality 
    WHEN 'AMERICAN'   THEN 1 
    WHEN 'BRITISH'   THEN 2
    WHEN 'GERMAN'    THEN 3 
    WHEN 'EGYPT'     THEN 4 
    WHEN 'PALESTINE' THEN 5 
    ELSE 6 END AS Nationality 
    
    0 讨论(0)
提交回复
热议问题