Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find substrings in SQL (Postgres)

Tags:

sql

postgresql

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

like image 780
O James Avatar asked Jul 03 '26 10:07

O James


1 Answers

Simply use like operator:

SELECT * from yourtable
where '12345' like '%' || Code 
like image 96
iljon Avatar answered Jul 06 '26 05:07

iljon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!