1

In particular, I'm looking for a way to more easily rewrite my like queries without having to use the "lower" function each time.

In SQL Server, my query would look like this:

WHERE (FIELD LIKE '%Foot%Locker%' or FIELD LIKE '%Foot%Action%' or FIELD LIKE '%Champs%')

In PostgreSQL, I must rewrite each query as such (if I want my query to capture both Foot Locker AND foot locker and any other caps driven permutation):

WHERE (lower(FIELD) LIKE lower('%foot%locker%') or lower(FIELD) LIKE lower('%foot%action%') or lower(FIELD) LIKE lower('%champs%')

This, of course, is very annoying. I have to rewrite 100s of queries. Is there an easy workaround?

0

1 Answer 1

4

Postgres has just what you are looking for ilike:

The key word ILIKE can be used instead of LIKE to make the match case-insensitive according to the active locale. This is not in the SQL standard but is a PostgreSQL extension.

(Documented here.)

So, you can do something like:

WHERE (field ILIKE '%foot%locker%' or FIELD ILIKE '%foot%action%' or FIELD) ILIKE '%champs%'

Or, if you prefer:

WHERE lower(field) ~ '(foot.*locker)|(foot.*action)|champ'
Sign up to request clarification or add additional context in comments.

1 Comment

you actually don't need the lower() if you use ~* instead of ~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.