开发者

SQL CASE Statement

开发者 https://www.devze.com 2023-02-22 00:44 出处:网络
There aretwo fields (BIT data type) in my database table PE and PER i need to wr开发者_运维知识库ite a sql case statement to perform the following task.

There are two fields (BIT data type) in my database table PE and PER i need to wr开发者_运维知识库ite a sql case statement to perform the following task.

when PER=0 then print 'Not Required' when PER=1 then check if PE=0 then print 'Not Completed' when PE=1 print 'Completed'

following is the query i have tried and which is not working

CASE pc.PER WHEN '0' THEN 'Not Required'
                                    WHEN '1' THEN (CASE pc.PE WHEN '0' THEN 'Not Completed'
                       WHEN '1' THEN 'Completed') end as ab 


I think it might be easier to write it as:

CASE
    WHEN pc.PER = '0' THEN 'Not Required'
    WHEN pc.PER = '1' AND pc.PE = '0' THEN 'Not Completed'
    ELSE 'Completed'
END

Alternatively you would have to add END after ... WHEN '1' THEN 'Completed' in your query:

CASE pc.PER WHEN '0' THEN 'Not Required'
            WHEN '1' THEN (CASE pc.PE WHEN '0' THEN 'Not Completed'
                                      WHEN '1' THEN 'Completed'
                           END)
END AS ab 


You are missing an end.

CASE pc.PER 
    WHEN 0 THEN 'Not Required'
    WHEN 1 THEN 
        CASE pc.PE 
            WHEN 0 THEN 'Not Completed'
            WHEN 1 THEN 'Completed'
        END
END as ab 


try the following statement instead:

CASE WHEN pc.PER =0 THEN 'Not Required'  
     WHEN pc.PER =1 THEN 
         CASE   WHEN pc.PE = 0 THEN 'Not Completed'
                WHEN pc.PE = 1 THEN 'Completed' 
     End 
End as ab
0

精彩评论

暂无评论...
验证码 换一张
取 消