I think you want something like this:
DECLARE @nw TABLE ( sn INT, [key] VARCHAR(100) )
INSERT INTO @nw
VALUES ( 1, 'and' ),
( 2, 'on' ),
( 3, 'of' ),
( 4, 'the' ),
( 5, 'view' )
DECLARE @s VARCHAR(100) = 'view This of is the Man';
WITH cte
AS ( SELECT sn ,
REPLACE(@s, [key], '') AS s
FROM @nw
WHERE sn = 1
UNION ALL
SELECT n.sn ,
REPLACE(s, n.[key], '') AS s
FROM @nw n
JOIN cte c ON c.sn + 1 = n.sn
)
SELECT TOP 1 @s =
REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ')
FROM cte
ORDER BY sn DESC
Output:
This is Man
First you recursively removing noise words from search string, and in the end a little trick to remove duplicate continuous spaces.
Then you can filter base table like:
SELECT * FROM TableName WHERE Title LIKE '%' + @s + '%'
May be you want to consider FULL TEXT SEARCH
? I suspect you also want to remove those noise words from base table while searching. It will be very slow. Full Text Search
is optimized for such type of work. It includes noise words
, stoplists
and more...
If you don't want to use Full Text Search, you can add additional column to your base table, which will hold the value from Title but without noise words and search based on that column.
But if you insist here is the full code for this:
DECLARE @t TABLE
(
SNo INT ,
Title VARCHAR(100)
)
INSERT INTO @t
( SNo, Title )
VALUES ( 1, 'women holding stack of gifts' ),
( 2, 'Rear view of a man playing golf' ),
( 3, 'Women holding gifts' ),
( 4, 'Women holding gifts' ),
( 5, 'Businessman reading a newspaper and smiling' ),
( 6, 'Hey This some what of is the Man from Chicago' )
DECLARE @nw TABLE
(
sn INT ,
[key] VARCHAR(100)
)
INSERT INTO @nw
VALUES ( 1, 'and' ),
( 2, 'on' ),
( 3, 'of' ),
( 4, 'the' ),
( 5, 'view' ),
( 6, 'some' ),
( 7, 'what' )
And the code:
DECLARE @s VARCHAR(100) = 'view This of is the Man';
WITH cte
AS ( SELECT sn ,
REPLACE(@s, [key], '') AS s
FROM @nw
WHERE sn = 1
UNION ALL
SELECT n.sn ,
REPLACE(s, n.[key], '') AS s
FROM @nw n
JOIN cte c ON c.sn + 1 = n.sn
)
SELECT TOP 1
@s = REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ')
FROM cte
ORDER BY sn DESC
;WITH cte
AS ( SELECT t.* ,
n.sn ,
REPLACE(t.Title, n.[key], '') AS s
FROM @t t
JOIN @nw n ON sn = 1
UNION ALL
SELECT c.SNo ,
c.Title ,
n.sn ,
REPLACE(c.s, n.[key], '')
FROM cte c
JOIN @nw n ON n.sn = c.sn + 1
)
SELECT *
FROM cte
WHERE REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ') LIKE '%' + @s + '%'
And the output:
SNo Title sn s
6 Hey This some what of is the Man from Chicago 7 Hey This is Man from Chicago