---
title: "Trim method in SQL Server"  
description: "SQL server does not have Trim method, but for trimming blank spaces (leading and trailing) from string we have used LTRIM and RTRIM method in SQL Serv"  
author: "AVADHESH PATEL"  
published: 2013-02-14  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/444/trim-method-in-sql-server  
category: "database"  
tags: ["database"]  
reading_time: 1 minute  

---

# Trim method in SQL Server

Trim method in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server)

SQL server does not have **Trim** method, but for [trimming](https://www.mindstick.com/forum/23312/eclipse-video-trimming) blank spaces (leading and [trailing](https://answers.mindstick.com/qa/105012/what-is-a-trailing-stop)) from string we have used **LTRIM** and **RTRIM** method in SQL Server. User can easily use **LTRIM**() and **RTRIM**() [together](https://yourviews.mindstick.com/view/80819/live-in-relationship-protecting-the-right-to-live-together) and simulate **TRIM**() functionality. For example

### Trim in Simple Query

### SELECT RTRIM(LTRIM(' SQL Server Trim() Demo ')) AS Trim_String;

It return ‘SQL Server Trim() Demo ’ string without white space.

### Trim using function

CREATE [FUNCTION](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) dbo.TRIM(@string VARCHAR(MAX))

RETURNS VARCHAR(MAX)

BEGIN

RETURN LTRIM(RTRIM(@string))

END

SELECT dbo.TRIM(' SQL Server Demo ') AS Trim_String;

### Trim using function with table in 2008

-- [Create Table](https://www.mindstick.com/articles/443/how-to-create-table-in-sql-server)

CREATE TABLE TrimDemo

(

ID TINYINT NOT NULL [IDENTITY](https://www.mindstick.com/articles/13090/icon-the-identity-of-your-brand) (1, 1),

StringCol VARCHAR(150) NOT NULL,

TrimmedCol AS LTRIM(RTRIM(StringCol))

) ON [PRIMARY]

GO

-- [Insert data](https://www.mindstick.com/forum/172/how-v-insert-data-into-table-and-display-into-gridview-in-aspx-cs-file-with-out-using-wizard) into table

INSERT INTO TrimDemo

([StringCol])

SELECT ' SQL Server'

UNION

SELECT 'SQL Server 2005 '

UNION

SELECT ' SQL Server 2008 '

UNION

SELECT 'SQL Server 2012'

GO

-- Select table

SELECT * FROM TrimDemo

GO

**Note:** If user inserts blank/white spaces between [two strings](https://www.mindstick.com/forum/159716/regex-match-all-characters-between-two-strings) then LTRIM and RTRIM not remove that, it’s removing only [starting](https://www.mindstick.com/blog/12024/things-you-need-to-know-when-starting-your-own-business) and ending blank/white spaces.

---

Original Source: https://www.mindstick.com/blog/444/trim-method-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
