简体   繁体   中英

How to remove trailing spaces like characters in SQL Server

I have written a simple select query to select a single row from a table using a field named “Name”. The Names are sequential and presented as 'RM001', 'RM002', 'RM003'…. This issue was that it didn't pick up 'RM004' with the following query

-- Trim Name Field
UPDATE [dbo].[RoutineMaintenanceTask] SET  = LTRIM(RTRIM([dbo].[RoutineMaintenanceTask].Name));

-- Select the record
SELECT   *
FROM     [dbo].[RoutineMaintenanceTask]
WHERE    Name = 'RM004'

在此处输入图片说明

When I was checking the length of the value using the following query, it showed me the length as 7

-- Check the length
select (Name), len(Name) AS TextLength
from [dbo].[RoutineMaintenanceTask]
where Name = 'RM004'

It is obvious that this name contains some characters before or after, but it is not a space.

Not only that, I examined the value through Visual Studio debugger and did not notice anything unusual.

在此处输入图片说明

Nevertheless, when I copy the value of the “Name” from SQL results pane and copy it to notepad++, with special characters on, I was able to see this.

在此处输入图片说明

Ultimately, I was able to fix this the issue by adding following code before the select statement

-- Remove the tail
UPDATE [dbo].[RoutineMaintenanceTask] SET Name = substring(Name,1,5);

I just need to know how I get to know what are the hidden characters in a case like this and how to eliminate it without using substring (Because in this case, it was easy because I knew the length).

PS- I understand that using the keyword of 'name' as a field of a table is not a good practise, but in this context there is nothing to do with that.

It was likely either char(9), char(10), or char(13) (tab,lf,cr; respectively).

You can read up on them here: https://docs.microsoft.com/en-us/sql/t-sql/functions/char-transact-sql?view=sql-server-2017

You can remove them using REPLACE() .

Such as:

DECLARE @VARIABLE VARCHAR(10)

SET @VARIABLE='RM004'+CHAR(10)+CHAR(10)

SELECT @VARIABLE, LEN(@VARIABLE)

SET @VARIABLE = REPLACE(@VARIABLE, CHAR(9),'')
SET @VARIABLE = REPLACE(@VARIABLE, CHAR(10),'')
SET @VARIABLE = REPLACE(@VARIABLE, CHAR(13),'')

SELECT @VARIABLE, LEN(@VARIABLE)
DECLARE @string VARCHAR(8000) = 'RM004
     ';

WITH 
    cte_n1 (n) AS (SELECT 1 FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) n (n)), 
    cte_n2 (n) AS (SELECT 1 FROM cte_n1 a CROSS JOIN cte_n1 b),
    cte_Tally (n) AS (
        SELECT TOP (DATALENGTH(@string))
            ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
        FROM
            cte_n2 a CROSS JOIN cte_n2 b
        )
SELECT 
    position = t.n,
    char_value = SUBSTRING(@string, t.n, 1),
    ascii_value = ASCII(SUBSTRING(@string, t.n, 1))
FROM
    cte_Tally t;

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM