SQL Syntax - Remove blank space Padding from string

I have data stored in a string column, and it has white space blank padding on either side:

table: person

name
'     john   '
' jane  '

I would like to remove the left and right space padding on either side - and the padding length is unknown.
Something like:?

Left(name, CHARINDEX (' ', name, CHARINDEX (' ', name) + 1) - 1)

Open in new window

robthomas09Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rushShahCommented:
try this..

SELECT ltrim(rtrim(name))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k_murli_krishnaCommented:
TRIM(name) will do the job for you.
0
k_murli_krishnaCommented:
Sorry TRIM is not there in SQL Server. rushShah is correct. You can also use:
SELECT rtrim(ltrim(name))
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

k_murli_krishnaCommented:
This removes all whitespace, including tabs, carriage returns and line feeds. Further, it can be easily modified to trim any set of characters – just make @charstotrim a parameter.

CREATE FUNCTION dbo.trim(@input varchar(8000)) returns varchar(8000) as
begin
declare @charstotrim varchar(100)
select @charstotrim =’ ‘+char(9)+char(10)+char(13)
while charindex(left(reverse(@input),1),@charstotrim) >0
select @input=left(@input,datalength(@input)-1)
while charindex(left(@input,1),@charstotrim) >0
select @input=substring(@input,2,datalength(@input)-1)
return @input
end
0
Rajkumar GsSoftware EngineerCommented:
To remove right & left spaces in all data, execute this query

UPDATE urTable SET urColumn = LTRIM(RTRIM(urColumn ))

Replace with your actual table name & column name to update

Now you can verify using this query

SELECT urColumn FROM urTable

Raj
0
robthomas09Author Commented:
thanks - will try and let you know
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.