SQL Server : RTRIM not working to remove empty spaces - sql-server

One of the column values in my tables have empty space at the end of each string.
In my select query, I am trying to trim the empty space at the end of string but the value is not getting trimmed.
SELECT
EmpId, RTRIM(Designation) AS Designation, City
FROM
tblEmployee
This is not trimming the empty space, not just this even the LTRIM(RTRIM(Designation) AS Designation is not working.
I also tried
CONVERT(VARCHAR(56), LTRIM(RTRIM(Designation))) AS [Designation]
Nothing is trimming the empty space at the end of the string...
Any help appreciated
EDIT
Thanks to suggestions in the comments, I checked what the last value was in the column using ASCII(). It is 160 is a non-breaking space.
How can I remove this non-breaking space?

How can I remove this non-breaking space?
Just replace it with '' and use CHAR() function as
SELECT REPLACE(YourColumn, CHAR(160), '')
FROM YourTable;

Since the value can contain non-breaking spaces you need to replace those with regular spaces before doing the trim:
SELECT
EmpId, RTRIM(REPLACE(Designation,char(160),' ')) AS Designation, City
FROM
tblEmployee

I faced the similar problem, Use below script to remove the space in case trim function not work -
SELECT LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE([ColumnName], CHAR(10),
CHAR(32)),CHAR(13), CHAR(32)),CHAR(160), CHAR(32)),CHAR(9),CHAR(32))))
FROM [TableName]
For more info visit this page - http://www.ashishblog.com/trim-not-removing-spaces-in-sql/

Related

RTRIM does not remove spaces at the end?

I have used the Query:
update Table
set Seg = RTRIM(Seg)
This still doesn't remove the extra spaces at the end? I really need to remove this as I am doing vlookups in Excel and it is causing problems.
The datatype of Seg column is (nchar(10), null)
Any help is appreciated.
You can right-trim an NCHAR(X) column all you want, values will always be the same length. Namely: X. The value will always be padded with spaces, so RTRIM is basically a no-op on a fixed width character column.
Also note that in string comparisons, trailing spaces are ignored.
To trim spaces from the end you should use
UPDATE
TableName
SET
ColumnName = RTRIM(ColumnName)
if you want to trim all spaces then use this
UPDATE
TableName
SET
ColumnName = LTRIM(RTRIM(ColumnName))

FOR XML PATH always adds trailing space to value

Using the FOR XML PATH structure to create a list of values,
I find that (annoyingly) it always adds a trailing space to selected values.
This ruins my attempts at providing my own delimiters - the trailing space is added after the column and delimiters have been concatenated.
For example:
SELECT country + '-' FROM countryTable...
results in the following string:
china- france- england-
Has anyone else seen this, and is there a way to stop it?
I don't think TRIM() will work, as that would be applied before the extra space is inserted...
I'm using SQL Server 2016.
Thanks
Ok, thanks to John C and his sample query I found the culprit.
I had a AS [data()] clause after the column name/delimiter.
Removing that removed the trailing space.
I don't know how/why but it did...
I suspect the data inside the country column, What if each value in Country column is having leading space. For XML PATH does not add any space to the data
Try this
SELECT RTRIM(LTRIM(country)) + '-' FROM countryTable...
You may have leading/trailing spaces and/or CRLFs. Perhaps this will help
Declare #countryTable table (country varchar(100))
Insert Into #countryTable values
(' china'), -- leading space
(char(13)+'france'), -- leading char(13)
(char(10)+'england') -- leading char(10)
Select Value=Stuff((Select Distinct '-' + ltrim(rtrim(replace(replace(country,char(13),''),char(10),'')))
From #countryTable
Where 1=1
For XML Path ('')),1,1,'')
Returns
Value
china-england-france
FOR XML PATH ... AS [data()] add to this from MS Help
If the path specified as column name is data(), the value is treated as an atomic value in the generated XML. A space character is added to the XML if the next item in the serialization is also an atomic value. This is useful when you are creating list typed element and attribute values.
When you write here ... AS something. Then something is used as open/closing markup tag for each selected value.
Add 2. Is possible concate in select clausule more fileds from each row. For other types than string type, value must be converted into string type CAST AS

How do I remove line feed characters when selecting data from SQL Server?

I insert data that contains a line feed character into the database. Then I retrieve that data. I am using this script to attempt to remove the line feed while selecting the data from SQL:
Select Replace(Replace stringname,char(10),'',char(32),'')) from tablename
The replace function seems to execute, but it does not remove the line feed correctly.
The syntax of your statment looks wrong, maybe you can try with something like this:
Select Replace(Replace(#str,CHAR(10),''),CHAR(13),'')
The inner replace relaces LF and the outer replace replace CR
Shouldn't it be Select Replace(Replace(stringname,char(10),''),char(13),'') from tablename?
Also you could use single replace Select Replace(stringname,char(13)+char(10),'') from tablename.
char(32) corresponds to a space symbol
(select Replace( (select REPLACE( ColName,CHAR(10),'')),char(13),''))
as ColAlias
from YourTable
Solved with following .
I had issues with in sql data which even can not be seen as well in sql, causing me problem in some jquery functions.
A line feed is CHAR(10); a carriage return is CHAR(13).
The following code will remove a line feed characters and replaces it with a zero-length string:
UPDATE Table_Name SET Field_Name = REPLACE(Field_Name,CHAR(10),'');
If you want remove CRLF from the end of a string you can use RTRIM in postgresql.
for update operation:
UPDATE tablename
SET columnname = RTRIM(RTRIM(columnname,chr(10)),chr(13))
WHERE columnname like '%' || chr(13) or columnname like '%' || chr(10)
or for select:
SELECT RTRIM(RTRIM(columnname,chr(10)),chr(13)) FROM tablename
if you want leading or both use LTRIM or BTRIM

How to trim everything after certain character in sql

I am trying to format the email address in my table by removing everything starting the #. Also I would like to replace the underscore with blank space.
For example:
FirstName_LastName#gmail.com
I would like the above email to be changed like this:
FirstName LastName
Here is my code but this trims everything after the # and that is what i want. But how can i replace the underscore with blank. I want all in one statement using the update function. How can I do that?
SELECT
left (Email, CHARINDEX('#',Email)-1)
FROM [Dashboard]
Thanks for the help
SELECT REPLACE(LEFT(Email, CHARINDEX('#',Email)-1),'_',' ')
FROM [DSR].[dbo].[RCA_Dashboard]
This can be helpful if you need to remove all after the last certain character:
Declare #String nvarchar(max) =
'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\log.ldf'
select reverse(substring(reverse (#String), CHARINDEX('\', reverse (#String))+1, len(reverse (#String))));

How can I use LTRIM/RTRIM to search and replace leading/trailing spaces?

I'm in the process of trying to clear out leading and trailing spaces from an NVARCHAR(MAX) column that is filled with prices (using NVARCHAR due to data importing from multiple operating systems with odd characters).
At this point I have a t-sql command that can remove the leading/trailing spaces from static prices. However, when it comes to leveraging this same command to remove all prices, I'm stumped.
Here's the static script I used to remove a specific price:
UPDATE *tablename* set *columnname* = LTRIM(RTRIM(2.50)) WHERE cost = '2.50 ';
Here's what I've tried to remove all the trailing spaces:
UPDATE *tablename* set *columnname* LIKE LTRIM(RTRIM('[.]')) WHERE cost LIKE '[.] ';
I've also tried different varations of the % for random characters but at this point I'm spinning my wheels.
What I'm hoping to achieve is to run one simple command that takes off all the leading and trailing spaces in each cell of this column without modifying any of the actual column data.
To remove spaces from left/right, use LTRIM/RTRIM. What you had
UPDATE *tablename*
SET *columnname* = LTRIM(RTRIM(*columnname*));
would have worked on ALL the rows. To minimize updates if you don't need to update, the update code is unchanged, but the LIKE expression in the WHERE clause would have been
UPDATE [tablename]
SET [columnname] = LTRIM(RTRIM([columnname]))
WHERE 32 in (ASCII([columname]), ASCII(REVERSE([columname])));
Note: 32 is the ascii code for the space character.
To remove spaces... please use LTRIM/RTRIM
LTRIM(String)
RTRIM(String)
The String parameter that is passed to the functions can be a column name, a variable, a literal string or the output of a user defined function or scalar query.
SELECT LTRIM(' spaces at start')
SELECT RTRIM(FirstName) FROM Customers
Read more: http://rockingshani.blogspot.com/p/sq.html#ixzz33SrLQ4Wi
LTrim function and RTrim function :
The LTrim function to remove leading spaces and the RTrim
function to remove trailing spaces from a string variable.
It uses the Trim function to remove both types of spaces.
select LTRIM(RTRIM(' SQL Server '))
output:
SQL Server
I understand this question is for sql server 2012, but if the same scenario for SQL Server 2017 or SQL Azure you can use Trim directly as below:
UPDATE *tablename*
SET *columnname* = trim(*columnname*);
SELECT RTRIM(' Author ') AS Name;
Output will be without any trailing spaces.
Name
——————
‘ Author’
The LTrim function to remove leading spaces and the RTrim function to remove trailing spaces from a string variable.
It uses the Trim function to remove both types of spaces and means before and after spaces of string.
SELECT LTRIM(RTRIM(REVERSE(' NEXT LEVEL EMPLOYEE ')))

Resources