Back to results for “Fundamentals”
SQL Server › T-SQL Fundamentals & Syntax

What are the differences between the various string functions available in T-SQL, such as SUBSTRING, CONCAT, and TRIM?

Intermediate

Answer

SUBSTRING extracts a portion of a string starting at a specific position, CONCAT combines multiple strings together while automatically handling NULL values gracefully, and TRIM removes unwanted leading and trailing spaces or specified characters from a string, together giving you a versatile toolkit for cleaning and manipulating text data directly within your queries.
SELECT CONCAT(FirstName, ' ', LastName) AS FullName,
  TRIM(Email) AS CleanEmail,
  SUBSTRING(PhoneNumber, 1, 3) AS AreaCode
FROM Customers;
Real-world example A customer data cleanup script combines several string functions together, building a clean full name, trimming whitespace from email addresses, and extracting area codes from phone numbers all within a single query.

Common follow-ups: How does CONCAT handle NULL values differently from using the plus operator to combine strings?;What characters does TRIM remove by default if none are specified?