LIKE in SQL Server: search text

LIKE is pattern match on text. % means any string. _ means one character. People use it for “name contains”.

Contains, starts with

SELECT customer_id, first_name, last_name, email
FROM Customers
WHERE last_name LIKE 'Ash%'          -- starts with Ash
   OR email LIKE '%@harbour.co';     -- ends with that domain
Leading %

LIKE '%son' cannot use a normal index well. Fine on Customers. Painful on ten million log rows. If you do it every morning, talk about full-text or a computed last-four column.

To search for a real % character, use ESCAPE or 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