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
The PostgreSQL LIKE operator is used to match text values against a pattern using wildcards. If the search expression can be matched to the pattern expression, the LIKE operator will return true, which is 1. The percent sign represents zero, one, or multiple numbers or characters.
PostgreSQL is a case-sensitive database by default, but provides various possibilities for performing case-insensitive operations and working with collations.
Use the citext module, which mostly mimics the behavior of a case-insensitive data type. Having loaded that module, you can create a case-insensitive index by CREATE INDEX ON groups (name::citext); .
s: It formats the argument value as a string. NULL values are treated as an empty strings. I: It treats the argument value as an SQL identifier. L: It makes the argument value as an SQL literal.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With