Search code examples
postgresqlpostgresql-9.3

Case statement based on the result Postgresql


Hi so I have a question regarding case statements and why certain statements dont work.

(Case WHEN LENGTH(input)<=5 THEN text LIKE '%'
  ELSE text = input)

When I did this, it didnt work and im still confused as to why. However the solution to this problem was

(Case WHEN LENGTH(input)<=5 THEN text LIKE Concat('%',input,'%')
  ELSE text = input END)

However My main question is below though I would like an answer to the above question and since they are identical I will leave it as one question.

I have a student who is still being reviewed for entering the school, however this school is german so i need to translate a few words for them since the system is in English

select s.fname, s.lastname, 
(Case when s.status like 'APPROVED' then s.status = 'Genehmigt'
when s.status like 'PENDING' then s.status = 'Anstehend.'
end) as Status
from Student

My results are giving me False in the status field so obviously im doing something wrong... And I know there are two types of case statement so am I using the recommended one?


Solution

  • The expression you have placed after then:

    s.status = 'Genehmigt'
    

    is not an assignment, it is a boolean expression, so it yields true or false. s.status = is just redundant.

    select 
        fname, 
        lastname, 
        case when status = 'APPROVED' then 'Genehmigt'
        when status = 'PENDING' then 'Anstehend.'
        end as status
    from student
    

    Note that like with a string without wildcards on the right side should be a simple comparison.