T-SQL reference › SUBSTRING, LEFT, RIGHT
SUBSTRING, LEFT and RIGHT in SQL Server
LEFT is the start. RIGHT is the end. SUBSTRING is a middle slice. At work: first part of a postcode, not a credit card.
Postcode area
SELECT customer_id,
LEFT(postcode, CHARINDEX(' ', postcode + ' ') - 1) AS area
FROM Customers;
LEFT(postcode, 4) is simpler if you always want four characters. The CHARINDEX version stops at the space.
SELECT SUBSTRING(email, CHARINDEX('@', email) + 1, 200) AS domain
FROM Customers
WHERE email LIKE '%@%';
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