1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/postgres/string-contains-another-string.md

592 B

String Contains Another String

You can check if a string contains another string using the position function.

> select position('One' in 'One Two Three');
 position
----------
        1

It returns the 1-based index of the first character of the first match of that substring.

> select position('Four' in 'One Two Three');
 position
----------
        0

If the substring doesn't appear within the string, then the result is 0.

Thus, you can determine if a string contains another string by checking if the value resulting from position is greater than 0.