Skip to content
Advertisement

How to find substrings in SQL (Postgres)

Let’s say I have a column called ‘code’ in my database table containing string data types.

I want a query that will return all rows with a ‘code’ value that is a substring of a given search string, with the added condition that the substring appears at the end of the search string.

For example, if the search string is ‘12345’, the query should return all rows with a ‘code’ value of:
12345
2345
345
45
5

Advertisement

Answer

Simply use like operator:

SELECT * from yourtable
where '12345' like '%' || Code 
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement