Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Ltrim rtrim no working with UPDATE


Ltrim rtrim no working with UPDATE

Author
Message
Sriram-288748
Sriram-288748
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 340
Hi Friends,

I am facing an Issue while writing an update statement. The statement is like below

update tablename
set colname = ltrim(rtrim(colname)
where filter condition

the colname is of type CHAR.

trying to clean up the data and cant change the data type of the column.

Please advice.

Regards,
Sriram

Sriram
Lowell
Lowell
SSC-Insane
SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)SSC-Insane (23K reputation)

Group: General Forum Members
Points: 23631 Visits: 39694
if you declare a char(50), and put a single character in it, because of it's datatype it will add 49 spaces on the end.
that's the expected behavior.
Varchar(50), on the other hand, will behave as you expect...trim it, and it will not contain spaces at the end.

Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Sriram-288748
Sriram-288748
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 340
Lowell,

Thats the expected behavoiur fine, but the point here is, maybe for example I can put it like below. The column is of length 5. and I have more than 10 k records in that.

the result that i get for few records when i do a copy paste is

'WDT'
' WDT '

Regards,
Sriram

Sriram
andrewd.smith
andrewd.smith
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1410 Visits: 3232
Are you sure that 1st character is a normal space (character code 32)?

declare @s1 char(10), @s2 char(10)
select @s1 = CHAR(160) + 'WDT' + CHAR(160)
select @s1, '[' + LTRIM(RTRIM(@s1)) + ']'

select @s2 = CHAR(32) + 'WDT' + CHAR(32)
select @s2, '[' + LTRIM(RTRIM(@s2)) + ']'


Sriram-288748
Sriram-288748
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 340
the problem that I am facing with this is application is using colname= 'WDT' and I have 10 records against that value out of which 9 were inserted earlier and 1 has been recently inserted. now I am getting only 9 records in the app instead of 10 which is creating an issue. and cannot do a code change in the app. so trying to update the data, all in vein Sad

Sriram
Sriram-288748
Sriram-288748
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 340
tried to directly update with 'WDT' even that is not wokring for that records. running out of ideas.

Sriram
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66711 Visits: 40690
That's why Andrew said to check and make sure that what look like spaces, actually are... it's a data problem... the trim functions will not remove all white-space characters... just spaces.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
andrewd.smith
andrewd.smith
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1410 Visits: 3232
What do you get if you run this (after changing columns and table names appropriately)?

SELECT DISTINCT 
[Code] = ASCII(SUBSTRING(LTRIM(columnName), 1, 1)),
[Character] = '[' + SUBSTRING(LTRIM(columnName), 1, 1) + ']'
FROM tableName
WHERE (columnName LIKE '%WDT%')


Sriram-288748
Sriram-288748
SSC Veteran
SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)SSC Veteran (294 reputation)

Group: General Forum Members
Points: 294 Visits: 340
I get he below sol.

87 [W]

Sriram
David Burrows
David Burrows
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8743 Visits: 9643
I would do the following to find the offending values

SELECT colname,CAST(colname as varbinary(5)) 
FROM tablename
WHERE colname LIKE '%WDT'
AND colname <> 'WDT'




Far away is close at hand in the images of elsewhere.

Anon.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search