Match only entire words with LIKE?

我是研究僧i 提交于 2019-12-28 03:53:24

问题


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.


回答1:


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)




回答2:


WHERE (@string LIKE 'doc %' OR @string LIKE '% doc' OR @string LIKE '% doc %' OR @string = 'doc')



回答3:


you can just use below condition:

(' '+YOUR_FIELD_NAME+' ') like '% doc %'

it works faster than other solutions.




回答4:


You could use some ORs, LIKE '% doc %' OR LIKE 'doc %' OR LIKE '% doc' OR LIKE 'doc'




回答5:


None of the answers worked for me (SQL Server 2008 R2).

But I managed to get it to work using somewhat of the answers shown below..

SELECT * FROM myTable AS v
WHERE v.Note LIKE '%[^a-zA-Z0-9]CONTainers[^a-zA-Z0-9]%'

Note: v.Note is a TEXT data type

This worked for me with the following samples..

  • containers
  • CONTAINERS
  • conTaInERS

But not when the word 'containers' was surrounded by other letters or numerics..

  • 001containers001
  • AAcontainersAA

Which is what the OP asked for in the first place.




回答6:


What about NOT LIKE '%doc%'?



来源:https://stackoverflow.com/questions/6283767/match-only-entire-words-with-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!