
POSTED: Jun 6, 2011 6:48 PM (GMT)
I would suggest using a function to do the “hard” logic.
Use a simple query to find the records containing the substring of interest, then apply the function to those to get just the ones where the substring is a “word”.
declare sSubString varchar(255); declare sWildCardString varchar(255); set sSubString = ‘bob’; set sWildCardString = ‘%’ + sSubString + ‘%’; SELECT * FROM YourTable WHERE Column like sWildCardString and MyFunction(sSubString, Column) = 1
The logic for the function is actually pretty simple:
function MyFunction ( psSubString varchar(255), psSourceString varchar(max)) return integer begin declare i integer; declare iFound integer; set i = 1; set iFound = 0; while i > 0 and iFound = 0 begin set i = CHARINDEX(psSubString, sSourceString, i); if i > 0 begin set iFound = 1; if i > 1 and CHARINDEX(substring(sSourceString,i,1),’abcdefghijklmnopqrstuvwxyz’) > 0 set iFound = 0; if i < length(sSourceString) and CHARINDEX(substring(sSourceString,i+length(sSubString),1),’abcdefghijklmnopqrstuvwxyz’) > 0 set iFound = 0; end; set i = i + 1; end; return iFound; end;
The function finds EACH occurrence of the substring (might be more than one), then checks to make sure that the occurrence is not preceded or followed by an alpha character, which will handle any typical embedded text substrings.

POSTED: Jun 7, 2011 1:08 AM (GMT)
Since some parts of an answer might change, what database product is being used?
Tom



















