Find a string in a Postgresql text array of strings

The problem:  You want to find or match a string that is located in a Postgresql text string array.

The text array of stings could look like this {Restroom, Locker, Entrance}.

In this example the table called "places"  has a column called "tags" that is our Postgresql text arrray.

The query you would want to use to find all Restrooms without having to have an exact match but use a like statement could look like this:

Select array_to_string(tags,';') from places where array_to_string(tags,';') like '%Rest%';

This will find all entries that match only the first for characters in Restroom

If you want to do this in Django as a raw query your query statement would look like:

Places.objects.raw("""Select id, array_to_string(tags,';') from appName_places where array_to_string(tags,';') like %s',['%Rest%']"""):

Comments

Popular Posts