Skip to content
Advertisement

What is wrong with my Postgres statement?

SELECT first_name, last_name, manager_id
    CASE manager_id
        WHEN manager_id IS null THEN "pip"
        End manager_id
    FROM assgnssql.employees;

I am trying to select list of employees, but i know some employees do not have manager_id, for these employees without manager_id (null) i want the result to display “pip” while for the rest it displays original info.

Advertisement

Answer

The code you want is probably:

SELECT first_name, last_name, manager_id
        (CASE WHEN manager_id IS null THEN 'pip' ELSE manager_id
         END) as manager_id
FROM assgnssql.employees;

Or more simply:

SELECT first_name, last_name, manager_id
       COALESCE(manager_id, 'pip') as manager_id
FROM assgnssql.employees;

The two significant issues are:

  • Your CASE syntax is messed up. Either you use comparisons or you have CASE <value>, but not both.
  • Strings are delimited by single quotes.
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement