使用LTRIM和RTRIM函数删除空格并不像我期望的那样工作:
示例:
SELECT RTRIM(LTRIM('TEST NAME '))
RESULT = 'TEST NAME ' DOES NOT WORK
SELECT RTRIM(LTRIM('TEST NAME '))
RESULT = 'TEST NAME' IT WORKS
答案 0 :(得分:4)
可能你有一些不是空格(制表符)的字符,或者你的字符串以carriage-return
/ null终止结尾。
答案 1 :(得分:2)
实际上,对于我使用SQL Server 2008 R2,它适用于您的两个示例。
证明:
SELECT '"' + RTRIM(LTRIM('TEST NAME ')) + '"'
SELECT '"' + RTRIM(LTRIM('TEST NAME ')) + '"'
将给予" TEST NAME"作为两者的输出。
您的真实数据可能包含除空格之外的其他内容,但您的示例并未说明这一点。
答案 2 :(得分:2)
使用此示例代码查找字符串中的任何隐藏字符
-- The @position variable holds the position of the character currently
-- being processed. The @nstring variable is the Unicode character
-- string to process.
DECLARE @position int, @nstring nvarchar(500);
-- Initialize the current position variable to the first character in
-- the string.
SET @position = 1;
-- Initialize the character string variable to the string to process.
-- Notice that there is an N before the start of the string, which
-- indicates that the data following the N is Unicode data.
SET @nstring = N'TEST NAME ';
-- Print the character number of the position of the string you are at,
-- the actual Unicode character you are processing, and the UNICODE
-- value for this particular character.
PRINT 'Character #' + ' ' + 'Unicode Character' + ' ' + 'UNICODE Value';
WHILE @position <= DATALENGTH(@nstring)
-- While these are still characters in the character string,
BEGIN;
SELECT @position,
CONVERT(varchar, SUBSTRING(@nstring, @position, 1)),
UNICODE(SUBSTRING(@nstring, @position, 1));
SELECT @position = @position + 1;
END;
来源https://docs.microsoft.com/en-us/sql/t-sql/functions/unicode-transact-sql
答案 3 :(得分:0)
您可以使用替换:
删除控制字符CHAR(10)和CHAR(13)SELECT RTRIM(LTRIM(REPLACE(REPLACE('TEST NAME ', CHAR(13), '')
,CHAR(10), '')
)
)