PostgreSQL – SELECT with IF Statement Error ‘Column Does Not Exist’

postgresqlpostgresql-11select

Database, application.
PostgreSQL 11.4 (Ubuntu 11.4-1.pgdg19.04+1),
Dbeaver 6.1.1.

What I need?
Return right values.

What I do.
Here is part of code.

select
if(available_count < 5, "neni skladem", "na sklade") as availability
from
...

Return this.

SQL Error [42703]: ERROR: column "neni skladem" does not exist

Where is the catch?

Thanks.

Best Answer

You have two errors in your statement:

  1. There is no IF in SQL, you need to use a CASE expression
  2. String constants are defined using single quotes in SQL ('), double quotes are for identifiers
select case 
          when available_count < 5 then 'neni skladem'
          else 'na sklade'
       end as availability
from ...