Martin
Martin

Reputation: 11041

Sql Server 2008 - Split Parameter by Space, Return Records LIKE All Split Results

I am passing in a @Description parameter to a stored procedure (VARCHAR). The parameter could be something like the book. I want to return records where the column contains both the and book.

These records would return:

I have a split function to split the @Description parameter by the space, but I have no idea how to include this in the where clause ... I also need to ignore the @Description parameter when it is NULL.

Disclaimer: I apologize if this was previously asked ... I searched for a while and didn't find anything.

Upvotes: 1

Views: 676

Answers (1)

Lamak
Lamak

Reputation: 70648

Well, if you want to have a result that have all the words on your parameter, you can do something like this:

SELECT A.YourFields
FROM YourTable A
INNER JOIN (SELECT *, COUNT(*) OVER() Total FROM dbo.Split(@Description)) B
ON A.YourField LIKE '%' + B.Data + '%'
GROUP BY A.YourFields
HAVING COUNT(*) = MAX(B.Total)

Upvotes: 2

Related Questions