I am using sql server and i have a table which have two fields
- Name
- Description
I want to select all those rows which contains following strings
('$','%' , 'ac cd' , 'hjd oih')
I am using it like this
select Name , description from table_name
where Description like any (' %$%',' %[%]%','%ac cd%' , '%hjd oih%')
now its giving error when i run this
Incorrect syntax near the keyword 'any'.
thanks ,
If you have multiple patterns
to search then its better to create a derived table
or temporary table
to store the patterns
DECLARE @patterns TABLE (
pattern VARCHAR(20)
);
INSERT INTO @patterns VALUES ('%$%'), ('%[%]%'), ('%ac cd%');
SELECT a.* FROM table_name a JOIN @patterns p ON (a.Description LIKE p.pattern);
If you have multiple columns to compare just add expression in the ON
clause
SELECT a.* FROM table_name a JOIN @patterns p ON (a.Description LIKE p.pattern)
OR (a.Column2 like p.pattern);
Updated: Using View
CREATE VIEW PatternSearch
AS
WITH Patterns (Patterns)
AS
(
Select '%Tool%'
union all
Select '%Sale%'
union all
Select '%ac cd%'
)
SELECT a.* FROM table_name a JOIN Patterns p ON (a.Description LIKE p.pattern)
GO
Not tested but you can do like this
Source