Search code examples
postgresqlcase

How do i use the like operator in a case select for Postgres SQL?


I would like to write an SQL statement with a CASE WHEN clause that uses the LIKE operator but I am not sure how to properly format the statement.

 SELECT
   services.id,
   (CASE services.description
     WHEN LIKE '%-'
       THEN services.amount * -1
     ELSE services.amount
   END) AS service_amount
 FROM services

Solution

  • Try it on this way:

    SELECT
       services.id,
       CASE WHEN services.description LIKE '%-'
            THEN services.amount * -1
            ELSE services.amount
       END) AS service_amount
     FROM services