Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-01-20
5
Medium Priority
?
547 Views
Last Modified: 2013-01-21
Is there any setting required, as sometimes ltrim and rtrim does'nt work.
0
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 53

Expert Comment

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

Assisted Solution

by:ValentinoV
ValentinoV earned 1000 total points
ID: 38800050
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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 38800138
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
ID: 38803644
tx
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38803784
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

715 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