I have an SQL query that joins few tables and I want to filter records using same LIKE condition for many columns of result set.
For example, I have columns t1.Name, t1.FullName, t1.Comment, t2.Name, t3.Description, etc in my query (t1, t2 and t3 are joined tables names) and I want to check whether t1.Name or t1.FullName or t1.Comment or t2.Name or t3.Description is LIKE ‘%sometext%’.
I’m just interested what SQL will be faster?
WHERE t1.FullName LIKE '%sometext%' OR t1.Comment LIKE '%sometext%' OR
t2.Name LIKE '%sometext%' OR t3.Description LIKE '%sometext%'
or
WHERE ISNULL(t1.Name,'') + '|' + ISNULL(t1.FullName,'') + '|' + ISNULL(t1.Comment,'') + '|' + ISNULL(t2.Name,'') + '|' + ISNULL(t3.Description,'') LIKE '%sometext%'
Or may be there is some even faster way?
I’m using MS SQL Server 2008 R2.
UPD: I’ve edited my second query to handle situations when some fields are NULL and when concatenation may contain pattern but fields separately don’t.
I would keep it written like this:
Several reasons for not using the latter, where faster doesn’t even come into play:
t1.FullName + ....may result in NULL – you have to handle thist1.FullName + ....may result in Text being found between t1.Comment and t2.Name, e.g.matcheshubble