I have data I cleaning up in an old data table prior to moving it to a new one. One of the fields has spaces in the column, right & left. I wrote the following code to address this and still have leading spaces?? The bulk of the data is clean when using this code but for some reason there are spaces prior to RT addresses... Has anyone else had this type of issue?
,CASE
WHEN PropStreetAddr IS NOT NULL
THEN (CONVERT(VARCHAR(28),PropStreetAddr))
WHEN PropStreetAddr is NOT NULL Then (Select LTrim(RTrim(PropStreetAddr)) As PropStreetAddr)
ELSE NULL END as 'PROPERTY_STREET_ADDRESS'
Sample output data:
1234 20th St
RT 1 BOX 2
560 King St
610 Nowland Rd
RT 1
1085 YouAreHere Ln
RT 24 Box 12
The most likely culprits are going to be 13 and/or 10 which are carriage return / line feed respectively. There other characters are what is causing the behavior you are seeing. The LTRIM and RTRIM functions are working perfectly fine, it is your data that is at fault here.
LTrim() and RTrim() Function in MS AccessIn LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. 2. RTrim() Function : It works same like LTrim() function but it will remove all trailing spaces from a string.
TRIM has one advantage over LTRIM and RTRIM — it can remove a single character from both ends of the string. LTRIM and RTRIM operate on only one end of the string.
LTRIM() Function in SQL Server. LTRIM() function helps to return remove all the space characters found on the left-hand side of the string. Parameter : string – The string from which the leading space character would be removed.
I've had the same problem - wrapping the string in a CAST(x as varbinary(64)) shows the hex and from this I see A000 - which I believe is non-breaking space.
To remove, try this (for UNICODE);
LTRIM(RTRIM(REPLACE(my-column, NCHAR(0x00A0), '')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With