Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match only entire words with LIKE?

Tags:

So 'awesome document' LIKE '%doc%' is true, because doc is a sub-string. But, I want it to be false while 'awesome doc' or 'doc awesome' or 'awesome doc awesome' should be true. How can I do with with a like?

I'm using sqlite, so I hope I don't have to use something that isn't available.

like image 611
Abhinav Sharma Avatar asked Jun 08 '11 18:06

Abhinav Sharma


People also ask

How to match full word regex?

To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.

How do I match a word in SQL?

SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.


1 Answers

How about split it into four parts -

[MyColumn] Like '% doc %'  OR [MyColumn] Like '% doc'  OR [MyColumn] Like 'doc %'  OR [MyColumn] = 'doc' 

Edit: An alternate approach (only for ascii chars) could be:

'#'+[MyColumn]+'#' like '%[^a-z0-9]doc[^a-z0-9]%' 

(You may want to take care of any special char as well)

It doesn't look like, but you may want to explore Full Text Search and Contains, in case that's more suitable for your situation.

See: - MSDN: [ ] (Wildcard - Character(s) to Match) (Transact-SQL)

like image 144
YetAnotherUser Avatar answered Nov 06 '22 23:11

YetAnotherUser