Solved

ltrim and rtrim sometimes doensn't work on sql server.

Posted on 2013-01-20
5
510 Views
Last Modified: 2013-01-21
Is there any setting required, as sometimes ltrim and rtrim does'nt work.
0
Comment
Question by:searchsanjaysharma
5 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
Can you define doesn't work?  specific errors
0
 
LVL 37

Assisted Solution

by:ValentinoV
ValentinoV earned 250 total points
Comment Utility
LTRIM/RTRIM will only remove spaces. Strings can contain other invisible characters which are not spaces. You may experiencing that.

Use the ASCII function on the first (or last) character in the string to check if it's actually a space.

select ASCII(' ') (with a space in between the quotes) returns 32.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
I confirm above. usually it's the "html" space (ascii code 160) which is the culprit.
if you do this:
ltrim(rtrim( replace(yourfield, chr(160), ' ') ))
it will trim those characters also

another gotcha is this: if the field is CHAR (and not VARCHAR), and update it using trim functions, it will will have the trailing spaces.
0
 

Author Closing Comment

by:searchsanjaysharma
Comment Utility
tx
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Is there any setting required, as sometimes ltrim and rtrim does'nt work.
Yeah, it is quite complex, but it involves reading the definition of LTRIM() and RTRIM() in SQL Server's BOL and (this is the important part) understanding how it works or "does'nt work".
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now