Mufasa
Mufasa

Reputation: 11

SQL regex that excludes non-alphabets

I am writing a sql query to look through list of urls. I only have very limited knowledge in regex and I have never used it in SQL.

Say there is a table called website and there is a column called url

what I am trying to do is I want to find url that has the word act.

select *
from website
where url like '%act%'

But I realized I do not want any alphabetical character right before or after the word act.

I know regex can give me what I need, but I just can't figure out how to use it in sql syntax.

Upvotes: 1

Views: 807

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269683

If you want only alphabetical characters in the string, then:

where url like '%act%' and url not like '%[^a-zA-Z]%'

If you only care about the characters just before and after:

where url like '%[^a-zA-Z]act[^a-zA-Z]%'

Upvotes: 1

Related Questions