As long as the words are separated (blank, /
or any other delimiter), this can be done with a string splitter and a hit count, but you won't find "Tail" in "ObfuscateTail". You'd need some CamelCase parsing additionally...
A rather easy workaround would be a LIKE
search with all the fragments, but this might bring back to much - and (for sure!) this won't be fast...
Try something like this:
DECLARE @mockupTable TABLE(ID INT IDENTITY, YourTarget VARCHAR(100));
INSERT INTO @mockupTable VALUES('51L MissleadingLENWord ObfuscateTail 10mg Apple')
,('Some other 51L with differing words');
DECLARE @search VARCHAR(100)='Apple 10mg/51L Tail';
WITH Parted AS
(
SELECT CAST('<x>' + REPLACE(REPLACE(@search,' ','/'),'/','</x><x>') + '</x>' AS XML) AS SearchFragmentsXML
)
,AllSearchWords AS
(
SELECT frgmnt.value(N'.',N'nvarchar(max)') AS Frg
FROM Parted
CROSS APPLY SearchFragmentsXML.nodes(N'/x') AS A(frgmnt)
)
SELECT ID
,COUNT(*) AS CountHits
,(SELECT COUNT(*) FROM AllSearchWords) AS CountFragments
FROM @mockupTable AS t
INNER JOIN AllSearchWords AS Frgs ON t.YourTarget LIKE '%' + Frgs.Frg + '%'
GROUP BY ID;
The result
ID CountHits CountFragments
1 4 4
2 1 4
The closer the "count of hits" is to the "count of fragments" the better.
UPDATE: A function (not recommended)
DROP FUNCTION dbo.YourSearch;
GO
CREATE FUNCTION dbo.YourSearch(@SearchIn VARCHAR(MAX), @SearchFor VARCHAR(100)='Apple 10mg/51L Tail')
RETURNS FLOAT
AS
BEGIN
DECLARE @rslt DECIMAL(10,4) =
(
SELECT CAST(COUNT(*) AS FLOAT) / MAX(SearchFragmentsXML.value('count(/x[text()])','float'))
FROM
(
SELECT CAST('<x>' + REPLACE(REPLACE(@SearchFor,' ','/'),'/','</x><x>') + '</x>' AS XML) AS SearchFragmentsXML
) AS Parted
CROSS APPLY SearchFragmentsXML.nodes(N'/x') AS A(frgmnt)
WHERE @SearchIn LIKE '%' + frgmnt.value(N'text()[1]',N'nvarchar(max)') + '%'
);
RETURN @rslt;
END
GO
DECLARE @mockupTable TABLE(ID INT IDENTITY, YourTarget VARCHAR(100));
INSERT INTO @mockupTable VALUES('51L MissleadingLENWord ObfuscateTail 10mg Apple')
,('Some other 51L with differing words');
SELECT t.*
,dbo.YourSearch(t.YourTarget,'Apple 10mg/51L Tail') AS HitCoeff
FROM @mockupTable AS t;
The result
ID YourTarget HitCoeff
1 51L MissleadingLENWord ObfuscateTail 10mg Apple 1
2 Some other 51L with differing words 0,25
Hint: It would help a lot, if you'd use a physical table with a SessionID
, where you fill in the fragments of your search string. Then you pass the SessionID
to the function and grab the fragments from there. This would - at least - avoid repeated splittings and could use result caching.