1

In a PostgreSQL function, what is the syntax to check if a variable passed in contains a specific letter?

The variable being passed to the function is comma separated list of letters and I want to check if this list contains the letter a or x.

I imagine the code would look something like this:

-- var typically looks like 'a,b,c,x'
if (some way of stripping anything other than a and x from var) ~* [ax]
  -- Do something else
end if;

I assume regex is the answer, I'm just not 100% sure about the syntax.

Thanks!

4

1 回答 1

5

检查字符串是否包含子字符串或字母的最简单方法是

if ( var like '%x%')
  -- Do something else
end if;

或者您可以使用您提到的正则表达式:

if ( var ~* 'x')
于 2013-10-02T14:58:44.773 回答