January 20, 2012 at 5:03 am
Hello comunity
I have a table where my field CCT is varchar(20), but on my desktop application some records append blanck space after my string:
ex: 71.9.909------------ (where '-' is black space
If i run this query :
update cu
set cct = rtrim(cct)
from cu WHERE
cct like '72.1.107%'
i try also :
update cu
set cct = rtrim(cct) --replace('72.1.107 ','','72.1.107')
from cu WHERE
cct like '72.1.107%'
both update don´t result. i mean field mantain the black space.
Could someone explain why.
Many thanks
Luis Santos
January 20, 2012 at 6:16 am
What does the following return?
SELECT ASCII(RIGHT(cct ))
FROM cu
WHERE cct LIKE '72.1.107%'
--Jeff Moden
Change is inevitable... Change for the better is not.
January 20, 2012 at 7:15 am
Hello
Your TSQL return error RIGHT, i run like this
SELECT ASCII(RIGHT(CCT,20))
from cu
WHERE cct like '72.1.107%'
QA return : 55
I solve my problem using this UPDATE:
update cu
set cct = substring(cct,1,8)
from cu WHERE
len(cct) > 8 and cct like '73.1%'
Any explanation ?
Best regards,
Luis Santos
January 20, 2012 at 7:34 am
Based on what you explained, all of them should work. Can you please provide us DDL & consumable data to explore your issue? I am sure it’s data driven issue.
January 20, 2012 at 8:20 am
luissantos (1/20/2012)
Any explanation ?
Yes, the white space trailing your varchar wasn't a space - it was a tab or some other white space character instead.
e.g.
DECLARE @test-2 TABLE(testID INT IDENTITY, cct VARCHAR(20));
INSERT INTO @test-2
SELECT ' 71.9.909 ';
SELECT *, LEN(cct) FROM @test-2;
WITH t1 AS (SELECT 1 N UNION ALL SELECT 1 N),
t2 AS (SELECT 1 N FROM t1 x, t1 y),
t3 AS (SELECT 1 N FROM t2 x, t2 y),
t4 AS (SELECT 1 N FROM t3 x, t3 y),
tally AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) N
FROM t4 x, t4 y),
Cleaner AS (SELECT testID, (SELECT CASE WHEN SUBSTRING(cct, N, 1) NOT LIKE '[^0-9]' OR SUBSTRING(cct, N, 1) = '.'
THEN SUBSTRING(cct, N, 1)
ELSE '' END + ''
FROM tally
WHERE N <= LEN(cct)
FOR XML PATH('')) AS clean
FROM @test-2)
UPDATE b SET cct = a.clean
FROM Cleaner a
INNER JOIN @test-2 b ON a.testID = b.testID;
SELECT *, LEN(cct) FROM @test-2;
January 20, 2012 at 9:40 am
luissantos (1/20/2012)
HelloYour TSQL return error RIGHT, i run like this
SELECT ASCII(RIGHT(CCT,20))
from cu
WHERE cct like '72.1.107%'
QA return : 55
I solve my problem using this UPDATE:
update cu
set cct = substring(cct,1,8)
from cu WHERE
len(cct) > 8 and cct like '73.1%'
Any explanation ?
Best regards,
Luis Santos
Apologies... I left out the 2nd operand of RIGHT. It should have been...
SELECT ASCII(RIGHT(cct,1))
FROM cu
WHERE cct LIKE '72.1.107%'
The "55" you got in return from your "doctored" query is the ASCII value of the number "7". I was trying to find out what the ASCII value for the right most character of the white space was. ASCII 32 is the value of a space character.
ASCII characters 48 through 57 represent the digits 0 thru 9. So, if it comes back with something besides those or 32, you have non-space white space on the end of your string.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply