Today I Learned

hashrocket A Hashrocket project

String Contains Another String

In postgres, you can check if a string contains another string using the position function.

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

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

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

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.

See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.