Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim spaces in string - LTRIM RTRIM not working

Tags:

sql

sql-server

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 -

Weird symbols

like image 223
InTheSkies Avatar asked Feb 05 '14 18:02

InTheSkies


People also ask

Why Ltrim is not working?

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.

Is trim same as Ltrim and Rtrim?

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.

How do you trim blank spaces in SQL?

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.

How do you remove leading and trailing spaces in Informatica?

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.


2 Answers

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)))) 
like image 53
Shridhar Avatar answered Oct 07 '22 05:10

Shridhar


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.

like image 43
Maheswaran Ravisankar Avatar answered Oct 07 '22 05:10

Maheswaran Ravisankar