T-SQL reference › CHARINDEX

CHARINDEX in SQL Server: find a character

CHARINDEX returns the position of a search string, starting at 1. Zero means it was not there. People use it to split email and names.

Domain after @

SELECT email,
       CASE WHEN CHARINDEX('@', email) = 0 THEN NULL
            ELSE SUBSTRING(email, CHARINDEX('@', email) + 1, 200) END AS domain
FROM Customers;

PATINDEX is the same idea with a LIKE pattern. CHARINDEX is enough for a single character.

Try it on Harbour & Co

Amy Nair (Finance Lead) needs this now: “Mailing list for the newsletter. Some names have stray spaces and emails have capitals. Clean it, no duplicates.” Write the query, run it, send the results. On the paid plan.

Take the ticket →

Learn it on a real ticket

Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.

Clock in free See Monday’s tickets