I tried this code -
UPDATE Table SET Name = RTRIM(LTRIM(Name))
Data type of Name is varchar(25)
None of the leading and trailing spaces get removed. When I copy-paste one such Name
, i get this -
"big dash" "space symbol" ABC001
Why is this happening and how do trim the spaces ?
EDIT -
The question has already been answered. I found one more table with this problem. I get "- value" when i copy a column of a row. When I press the enter key at end of this copy-pasted value, i see more dashes. See image below -
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.
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.
SQL Server TRIM() FunctionThe TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.
To trim the leading zero and space from the data contained in a source field, use the following: LTRIM(LTRIM(RTRIM(columnname)),0) - Use this when you want to trim the space from leading and trailing (inner ltrim and rtrim function), and then ltrim to remove the leading zero.
Kindly use below query it will remove space new line etc..
select LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(Name, CHAR(10), CHAR(32)),CHAR(13), CHAR(32)),CHAR(160), CHAR(32)),CHAR(9),CHAR(32))))
I suspect, some non readable(Non-ascii characters) inside the name column, that might not get removed as part of TRIM
calls.
select convert(varbinary, Name) from table
Reading the HEX
output from above query should reveal the same.
Kindly read this to find how to write functions to remove such characters.
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