I'm trying to create a stored procedure which does a search and calculates according to the matches a score.
DECLARE @Search VARCHAR(MAX) = 'John Doe Washington';
SELECT
[address].Name,
[address].Street,
[address].City,
[address].ZipCode,
[address].Country,
IIF([address].[Name] LIKE '%'+@Search+'%', 1000, 0)
+ IIF([address].[Street] LIKE '%'+@Search+'%', 800, 0)
+ IIF([address].[City] LIKE '%'+@Search+'%', 750, 0)
+ IIF([address].[ZipCode] LIKE '%'+@Search+'%', 300, 0)
+ IIF([address].[Country] LIKE '%'+@Search+'%', 250, 0) AS [Score]
FROM [dbo].[Addresses] AS [address];
Well this works, if I just use e.g. "John Doe" as the column Name
may contain it.
But I want to split the @Search
parameter later by comma, which works with a function [dbo].[splitstring](@stringToSplit VARCHAR(MAX))
which I found here. The function splits by comma and creates multiple rows.
So now I want to change my query, to make multiple search words work.
What I would want is a LIKE IN(SELECT '%'+Value+'%' FROM dbo.splitstring(@Search))
which doesn't exist in MS SQL Server unfortunately.
Also if multiple words matches the column, the score should be higher. That means e.g. the column contains "John Doe", the first word "John" matches with 1000 points, the second word "Doe" matches with an additional 1000 points. The third one, "Washington" doesn't match, so there will be 0 points for this one.
Is there a way to do this or is this not possible at the moment in MS SQL Server?