I have a column that seems to have a space at the end. I tried using ltrim and rtrim and it doesn't work.
I tried running this query:
DECLARE @Val NVARCHAR(250) DECLARE @i INT SET @Val = (select companyname from companies where companyid=3839) SET @i = 0 WHILE @i <= DATALENGTH(@Val) BEGIN PRINT SUBSTRING(@Val, @i, 1) + ' : ' + CAST(ASCII(SUBSTRING(@Val, @i, 1)) AS VARCHAR(3)) SET @i = @i + 1 END and I see the last 2 rows are 32 and 0 = char(32) and char(0).
I tried to use replace:
select replace(replace(companyname, char(0),''), char(32), '') from companies where companyid = 3839 But it also does not work.
Any other solutions?
- Column type:
nvarchar(250) - Column collation:
Hebrew_CI_AS - SQL Server version: Microsoft SQL Server 2008 R2 (RTM)
10.50.1600.1 (X64) Express Edition with Advanced Services (64-bit)
on Windows NT 6.1 (Build 7601: Service Pack 1)
Also tried this and I still see a space at the end:
Set @StringToClean = Replace(@StringToClean,CHAR(0),CHAR(32)); --Horizontal Tab Set @StringToClean = Replace(@StringToClean,CHAR(9),CHAR(32)); --Line Feed Set @StringToClean = Replace(@StringToClean,CHAR(10),CHAR(32)); --Vertical Tab Set @StringToClean = Replace(@StringToClean,CHAR(11),CHAR(32)); --Form Feed Set @StringToClean = Replace(@StringToClean,CHAR(12),CHAR(32)); --Carriage Return Set @StringToClean = Replace(@StringToClean,CHAR(13),CHAR(32)); --Column Break Set @StringToClean = Replace(@StringToClean,CHAR(14),CHAR(32)); --Non-breaking space Set @StringToClean = Replace(@StringToClean,CHAR(160),CHAR(32)); Set @StringToClean = LTRIM(RTRIM(@StringToClean));