简体   繁体   中英

SQL Server wildcard select with a twist

I am extracting some wildcards from a string type column using certain keywords but for certain keywords in my list i am getting some false positives which I do not want in my output. Some of the keywords in my wildcard select is 'old', 'older' and 'age'

select * from DESCRIPTIONS..LONG
where (DESCR like '% old %'
or DESCR like '% older %'
or DESCR like '% age %'
or DESCR like '%old%'
or DESCR like '%older%'
or DESCR like '%age%')

I want to extract only rows that contain these absolute words but I end up returning strings that include 'management', 'image', 'cold', 'colder' etc. I could remove these true negatives by not looking for the below

DESCR like '%old%'
or DESCR like '%older%'
or DESCR like '%age%'

but in that process I am excluding rows that have special characters like period, comma, slash etc. which are true positives Eg i would miss strings ending in 'age.' or 'old.' or 'older,' or 'age' when it is the last word in the string without a trailing space.

How do I exclude true negatives and false positives and only get all true positives?

here is a complete list of my keywords separated by a comma.

keywords: newborn, newborns, infant, infants, year, years, child, children, adult, adults, pediatric, old, older, young, younger, age

Thanks

Assuming that spaces delineate the words, you can use this trick:

select *
from DESCRIPTIONS..LONG
where ' ' + DESCR + ' ' like '% old %' or
      ' ' + DESCR + ' ' like '% older %' or
      ' ' + DESCR + ' ' like '% age %';

I suggest you to start looking at the LIKE Syntax from Microsoft: https://msdn.microsoft.com/en-us/library/ms179859.aspx

Are you searching for a free text field? You could use the [] syntax:

SELECT FROM DESCRIPTION..LONG
    WHERE DESCR LIKE '%[ "\/-]age[,.:;'' "\/-]%'

You put inside square brackets everything you accept in that position, resolving your issues with punctuation.

You need to make your regular expression more complicated:

LIKE '%[^a-z]old[^a-z]%'

What this will do is search for the word "old" without any letters directly before or after it (the ^ has the meaning of "not" in regular expressions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM