I have a table which contains Email Domains separated by semi-colons.
Domain
------
gmail.com;googlemail.com
hotmail.com;hotmail.co.uk;live.co.uk
I am currently running the following SQL:
SELECT [Id]
FROM [DomainGroup]
WHERE [Domain] LIKE '%' + (SELECT RIGHT('anemail@gmail.com', CHARINDEX('@', REVERSE('anemail@gmail.com')) - 1)) + '%'
Although this is working, I was thinking that maybe using PATINDEX would be better, from both performance and readability. The email address is actually a variable, but I’ve put it in to show what I’m trying to achieve.
I think the performance benefit of
PATINDEXvsLIKEis at best ambiguous, particularly in your case given that normalizing the delimited values to individual rows in an indexed column would give more of a performance boost than anything else.If you can’t change design & want to use
PATINDEX, then I would pre-compute the pattern;Then use that in the where clause;
(I padded with
;delimiters soaaa@bbb.comwould not matchaaabbb.com;)