SimpleGuy
SimpleGuy

Reputation: 2894

SELECT CASE in MySQL

I am running a simple query having SELECT CASE

SELECT DEPT_ID, DEPT_NAME = 
    CASE DEPT_NAME
        WHEN 'PBG' THEN 'Best Dept'
        ELSE 'Usual Dept'
    END
FROM DEPARTMENTS;

However, the output is not satisfactory. All 0 is coming in output

enter image description here

What is wrong in query ?

Otherwise the table has below data

SELECT * FROM DEPARTMENTS;

enter image description here

Upvotes: 2

Views: 102

Answers (1)

jarlh
jarlh

Reputation: 44776

Move the column alias DEPT_NAME. It should be placed after the case expression, not before:

SELECT DEPT_ID, 
    CASE DEPT_NAME
        WHEN 'PBG' THEN 'Best Dept'
        ELSE 'Usual Dept'
    END as DEPT_NAME 
FROM DEPARTMENTS;

Upvotes: 5

Related Questions