T-SQL reference › TRY_CAST

TRY_CAST in SQL Server: convert without blowing up

CAST on a dirty column stops the whole query at the first bad value. TRY_CAST returns NULL for that row and keeps going. That is what you want on an import.

A messy quantity

SELECT product_name,
       TRY_CAST(raw_qty AS int) AS qty
FROM ImportLines;

Rows that say “n/a” become NULL qty. You can find them with WHERE TRY_CAST(raw_qty AS int) IS NULL AND raw_qty IS NOT NULL.

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