简体   繁体   English

如何在SQL Server中删除字符之类的尾随空格

[英]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”. 我编写了一个简单的选择查询,使用名为“ Name”的字段从表中选择一行。 The Names are sequential and presented as 'RM001', 'RM002', 'RM003'…. 名称是连续的,并显示为“ RM001”,“ RM002”,“ RM003”…。 This issue was that it didn't pick up 'RM004' with the following query 问题是它没有通过以下查询获得“ RM004”

-- 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 当我使用以下查询检查值的长度时,显示为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. 不仅如此,我还通过Visual Studio调试器检查了该值,没有发现任何异常。

在此处输入图片说明

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. 但是,当我从SQL结果窗格中复制“名称”的值并将其复制到具有特殊字符的notepad ++时,我能够看到这一点。

在此处输入图片说明

Ultimately, I was able to fix this the issue by adding following code before the select statement 最终,我能够通过在select语句之前添加以下代码来解决此问题

-- 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. PS-我知道使用关键字“ name”作为表的字段不是一个好习惯,但是在这种情况下,这与之无关。

It was likely either char(9), char(10), or char(13) (tab,lf,cr; respectively). 可能是char(9),char(10)或char(13)(分别为tab,lf,cr;)。

You can read up on them here: https://docs.microsoft.com/en-us/sql/t-sql/functions/char-transact-sql?view=sql-server-2017 您可以在这里阅读它们: https : //docs.microsoft.com/zh-cn/sql/t-sql/functions/char-transact-sql?view=sql-server-2017

You can remove them using REPLACE() . 您可以使用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;

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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