Skip to main content

How Do You Do Ltrim And Rtrim In SQL Server?

by
Last updated on 3 min read

What's the quick fix for trimming spaces in SQL Server?

Use LTRIM() for leading spaces, RTRIM() for trailing spaces, and TRIM() for both. Try SELECT LTRIM(RTRIM(' SQL Server '))—it spits out 'SQL Server'. To clean up a whole column, run UPDATE table_name SET column_name = RTRIM(column_name).

What does LTRIM() actually do?

LTRIM() strips away leading spaces from the left side of a string. Think of it as the digital equivalent of grabbing the left edge of your text and yanking off any extra padding. RTRIM() does the same thing but on the right side for trailing spaces. And TRIM()? It’s the combo move—hits both sides at once. These functions are lifesavers when you’re wrestling with messy data, like when you import from a wonky CSV or a user pastes in text with invisible whitespace. (Seriously, users love adding extra spaces.)

How do I use LTRIM and RTRIM step by step?

Using LTRIM and RTRIM in SQL Server (2022 or later)

  1. Fire up SQL Server Management Studio (SSMS) 19.x or later.
  2. Connect to your database—no surprises here.
  3. Run this to ditch leading spaces:
    SELECT LTRIM('   LeadingSpaces   ') AS CleanedString;
    You’ll get: LeadingSpaces
  4. Now try this for trailing spaces:
    SELECT RTRIM('   TrailingSpaces   ') AS CleanedString;
    Result: TrailingSpaces
  5. Want to nuke both ends? Chain them together:
    SELECT LTRIM(RTRIM('   BothSides   ')) AS CleanedString;
    Boom: BothSides
  6. Apply it to a real column:
    UPDATE Employees SET LastName = RTRIM(LastName);
    This zaps trailing spaces from every entry in the LastName column.

Using TRIM in SQL Server (2017 and later)

SQL Server 2017+ finally got the ANSI SQL TRIM() function, so you can wipe both sides in one go:

SELECT TRIM('   FullTrim   ') AS CleanedString;
Result: FullTrim

What if LTRIM and RTRIM don’t seem to work?

1. Watch out for sneaky whitespace

Tabs (\t), carriage returns (\r), or line feeds (\n) often slip through. Zap them with REPLACE():

SELECT REPLACE(REPLACE(REPLACE(column_name, CHAR(13), ''), CHAR(10), ''), CHAR(9), '') AS CleanedString
FROM YourTable;

2. Trim by pattern instead of just spaces

Need to strip specific characters from the start or end? Combine PATINDEX with STUFF:

-- Wipe all leading commas
SELECT STUFF(',,,Hello', 1, PATINDEX('%[^,]%', ',,,Hello') - 1, '')
AS TrimmedString;

3. Wrap it in a view or stored procedure

If you’re doing this often, build a view or procedure so everyone uses the same cleanup rules:

CREATE VIEW CleanedEmployees AS
SELECT EmployeeID, LTRIM(RTRIM(FirstName)) AS FirstName, LTRIM(RTRIM(LastName)) AS LastName
FROM Employees;

How can I stop whitespace problems before they start?

Prevention beats cure, so here’s how to keep your data clean:

  • Validate at the app level. Add input constraints or form checks to block leading/trailing spaces before they hit the database.
  • Always trim in SELECT queries. Get in the habit of wrapping string columns with TRIM() whenever you display or export data.
  • Lock it down in the schema. Use CHECK constraints or generated columns to force trimmed values:
    ALTER TABLE Customers ADD CleanName AS LTRIM(RTRIM(Name)) PERSISTED;
  • Keep an eye on ETL pipelines. Make sure import scripts run LTRIM(RTRIM()) on every text field during ingestion.

Bottom line: trimming isn’t just about tidy output—it keeps your indexes sharp and your comparisons fast. Microsoft agrees: untrimmed strings can bloat indexes and slow down searches in big tables.

Edited and fact-checked by the TechFactsHub editorial team.
David Okonkwo
Written by

David Okonkwo holds a PhD in Computer Science and has been reviewing tech products and research tools for over 8 years. He's the person his entire department calls when their software breaks, and he's surprisingly okay with that.

What Is The Marketing Mix Used For?How Does A Buy Down Mortgage Work?