Will there be any other ways instead of writing the same criteria multiple times?
SELECT * FROM tblEmployees E
WHERE E.CurrentAddress LIKE '%dan%' OR
E.Email1 LIKE '%dan%' OR
E.Email2 LIKE '%dan%' OR
E.LatinName LIKE '%dan%'
Will there be any other ways instead of writing the same criteria multiple times?
SELECT * FROM tblEmployees E
WHERE E.CurrentAddress LIKE '%dan%' OR
E.Email1 LIKE '%dan%' OR
E.Email2 LIKE '%dan%' OR
E.LatinName LIKE '%dan%'
There are other ways, but yours is probably the most efficient already. You could always do something like:
SELECT *
FROM tblEmployees
WHERE CurrentAddress + Email1 + Email2 + LatinName LIKE '%dan%'
If some of the columns are NULL
, you could use ISNULL([field], '')
.
However, as @MitchWheat pointed out, it's not exactly the same query, since a field could end
by d
and the next field could start by an
.
If none of the values are NULL
, you could do:
SELECT *
FROM tblEmployees E
WHERE concat(E.CurrentAddress, ' ', E.email1, ' ', E.email2, ' ', E.LatinName) LIKE '%dan%';
Agree with plalx on this one, but if you're worried about typing '%dan%' multiple times, use a variable:
BEGIN
DECLARE @p_search NVARCHAR(50)
SET @p_search = '%dan%'
SELECT *
FROM tblEmployees E
WHERE E.CurrentAddress LIKE @p_search
OR E.Email1 LIKE @p_search
OR E.Email2 LIKE @p_search
OR E.LatinName LIKE @p_search
END
GO