I'm wondering how I would go about checking if any item within a list is contained within the name of a column that I'm trying to select.
Suppose I have a list like: list = ['apple', 'banana', 'cat']
. How can I check if a column's name contains any items in that list?
I was originally thinking something similar to:
SELECT column_name,
CASE WHEN column ('apple', 'banana', 'cat') IN column_name THEN true ELSE NULL END AS flag
FROM information_schema.columns;
But obviously this doesn't work. I was looking into the SUBSTR
function as well but not sure how this would work with a list of values.
Also what I've provided is more of a minimum reproducible example. The actual list will contain around 40 elements. Would appreciate any help, thanks!
You can use regular expressions:
SELECT column_name,
(CASE WHEN REGEXP_LIKE(column_name, 'apple|banana|cat') THEN true ELSE NULL
END) AS flag
FROM information_schema.columns;