I was wondering if it's possible to do a query using the IN clause where the options inside it are LIKE clauses, for example I have my existing SQL which returns the same results as I intend it just seems like a round about way to do it.
SELECT *
FROM pg_stat_activity
WHERE application_name NOT LIKE '%psql%'
AND (current_timestamp - state_change) > INTERVAL '30 minutes'
AND state IN (
SELECT state
FROM pg_stat_activity
WHERE state LIKE '%idle%'
OR state LIKE '%disabled%'
)
Is there a way to replace with something along the lines of
SELECT *
FROM pg_stat_activity
WHERE application_name NOT LIKE '%psql%'
AND (current_timestamp - state_change) > INTERVAL '30 minutes'
AND state IN ('%idle%', '%disabled%')
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.
PostgreSQL provides you with two wildcards: Percent sign ( % ) matches any sequence of zero or more characters. Underscore sign ( _ ) matches any single character.
LIKE and ILIKE allow pattern matching within character-based column data. Their syntax is identical, but LIKE is case-sensitive, while ILIKE is case-insensitive.
The % operator lets you compare against elements of an array, so you can match against any part of the name.
Use SIMILAR TO instead of LIKE
AND state SIMILAR TO '%(idle|disabled)%'
https://www.postgresql.org/docs/9.0/static/functions-matching.html
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