( I don't have sql-server installation to try CONTAINS
. You can replace the column LIKE '%string%'
with CONTAINS(column, 'string')
and try.)
See all queries here.
Another update - After reading the other answers and the manual, it seems that you don't need parenthesized values in the contains string unlike I expected. So this should work too - (you may even try ' | '
instead of ' OR '
SELECT CO.name, CA.description FROM company CO
INNER JOIN category CA
ON CA.CategoryId = CO.CategoryId
WHERE CONTAINS((CO.name,CO.description), REPLACE('ABC 25 SuperMarket', ' ', ' OR '))
AND
CONTAINS(CA.description, REPLACE('ABC 25 SuperMarket', ' ', ' OR '))
If it complains about syntax error near replace, you can create a search string DECLARE @SearchString varchar(MAX) = REPLACE('ABC 25 SuperMarket',' ', ' OR ')
and then use it in place of replace(......)
as second argument.
Update as per your modified question -
Firstly, you should be moving the logic to application level if possible. I think, it is too much to handle it here. I have come up with this query, but note that this will split each word and search for it in both name
and description
so you will end up getting a few more results than what you might think. For e.g. this will return all Supermarket
which have either ABC
or 24
in their name compared to returning only one Supermarket
with name ABC 24
in my previous query. This should actually help you out because, as per you, user might just type "ABC Supermarket 24" or "24 ABC Supermarket" or ...
DECLARE @SearchString varchar(MAX) = 'ABC 24 SuperMarket'
DECLARE @separator varchar(MAX) = ' '
DECLARE @Like1 varchar(MAX) = 'CO.name LIKE'
DECLARE @Like2 varchar(MAX) = 'CA.description LIKE'
DECLARE @WHERE1 varchar(MAX) = '( ' + @Like1 + ' ''%' +
REPLACE(@SearchString,@separator,'%'' OR ' + @Like1 + ' ''%')+'%'')'
DECLARE @WHERE2 varchar(MAX) = '( ' + @Like2 + ' ''%' +
REPLACE(@SearchString,@separator,'%'' OR ' + @Like2 + ' ''%')+'%'')'
DECLARE @QueryString varchar(MAX) =
CONCAT('SELECT CO.name, CA.description FROM company CO
INNER JOIN category CA
ON CA.CategoryId = CO.CategoryId
WHERE ', @WHERE1, ' AND ', @WHERE2)
exec(@QueryString);
If you output @WHERE1
you should see
( CO.name LIKE '%ABC%' OR CO.name LIKE '%25%' OR CO.name LIKE '%SuperMarket%')
As I said before you might want to try using CONTAINS
with parenthesized values like
DECLARE @SearchString varchar(MAX) = 'ABC 25 SuperMarket'
DECLARE @separator varchar(MAX) = ' '
DECLARE @WHEREString varchar(MAX) = '''"' +
REPLACE(@SearchString, @separator, '" OR "')+'"'''
SELECT CO.name, CA.description FROM company CO
INNER JOIN category CA
ON CA.CategoryId = CO.CategoryId
WHERE CONTAINS((CO.name,CO.description), @WHEREString)
AND
CONTAINS(CA.description, @WHEREString)
If you output @WHEREString
you should see
'"ABC" OR "25" OR "SuperMarket"'
Previous answer:
This will assumes that the word after the last space is the description
and rest is the `name.
You can split the search string and use them as shown below. This query is using like
as I don't have a sql-server installation.
DECLARE @SearchString VARCHAR(100) = 'ABC 24 Supermarket'
DECLARE @searchLength int = len(@SearchString)
DECLARE @searchReverse VARCHAR(100) = reverse(@SearchString)
SELECT CO.name, CA.description FROM company CO
INNER JOIN category CA
ON CA.CategoryId = CO.CategoryId
WHERE CO.name LIKE concat( '%', SUBSTRING(@SearchString,0,@searchLength-charindex(' ',@searchReverse)+1), '%')
AND
CA.description LIKE concat( '%', SUBSTRING(@SearchString,@searchLength-charindex(' ',@searchReverse)+2,@searchLength), '%')
This should work. Please note that the where clause is using AND instead of OR.
DECLARE @SearchString VARCHAR(100) = 'ABC 24 Supermarket'
DECLARE @searchLength int = len(@SearchString)
DECLARE @searchReverse VARCHAR(100) = reverse(@SearchString)
DECLARE @company VARCHAR(100) = SUBSTRING(@SearchString,0,@searchLength-charindex(' ',@searchReverse)+1)
DECLARE @category VARCHAR(100) = SUBSTRING(@SearchString,@searchLength-charindex(' ',@searchReverse)+2,@searchLength)
SELECT CO.name, CA.description FROM company CO
INNER JOIN category CA
ON CA.CategoryId = CO.CategoryId
WHERE CONTAINS((CO.name, CO.description), @company)
AND
CONTAINS(CA.description , @category)