简体   繁体   English

SQL 服务器CONCAT使用时加空格

[英]SQL Server CONCAT adds blank spaces when used

I've tried to use CONCAT function of some fields in a table;我尝试使用表中某些字段的 CONCAT function; in order to get a string that I need to compare onto another field from different table.为了得到一个字符串,我需要比较来自不同表的另一个字段。

However when I use the function it's like it random adds spaces between the fields and then I cannot use this result to compare.但是,当我使用 function 时,它就像在字段之间随机添加空格,然后我无法使用此结果进行比较。

I've tried:我试过了:

SELECT CONCAT([STC_GL-STC].[ZZGL_Desc_Group_5D],'-',
              [STC_GL-STC].[ZZCostCentreGroup],'-',
              AS RESULT
FROM [STC_GL-STC];

As an example of result:作为结果示例:

'Compras - RM -MATERIA PRIMA -'

(Please note the blank spaces in the second and third (-). (请注意第二个和第三个 (-) 中的空格。

I would need to obtain:我需要获得:

'Compras - RM-MATERIA PRIMA-'

I've checked the values in the fields and there is no blank spaces at the end on fields ZZGL_Desc_Group_5D, ZZCostCentreGroup.我检查了字段中的值,字段 ZZGL_Desc_Group_5D、ZZCostCentreGroup 的末尾没有空格。

I've also tried:我也试过:

SELECT CONCAT_WS('-',[ZZGL_Desc_Group_5D],[ZZCostCentreGroup]) AS RESULT
FROM [STC_GL-STC]

With same result.结果相同。

And finally I tried to remove blank spaces using RTRIM and LTRIM using the following:最后我尝试使用 RTRIM 和 LTRIM 使用以下方法删除空格:

SELECT CONCAT(LTRIM(RTRIM([STC_GL-STC].[ZZGL_Desc_Group_5D])),
              LTRIM(RTRIM('-')),
              LTRIM(RTRIM([STC_GL-STC].[ZZCostCentreGroup]))) AS RESULT
FROM [STC_GL-STC]
ORDER BY RESULT ASC;

And even with LTRIM and RTRIM functions on that field, I still getting the same result.即使在该字段上使用 LTRIM 和 RTRIM 函数,我仍然得到相同的结果。

How to get rid of this behaviour and of the blank spaces?如何摆脱这种行为和空白? Is there another way to build that string?还有另一种构建该字符串的方法吗?

Kind Regards and many thanks in advance,亲切的问候和非常感谢提前,

Long time ago I created a udf function to remove white spaces.很久以前,我创建了一个 udf function 来删除空格。

It is based on the 'magic' of the XML xs:token data type.它基于 XML xs:token数据类型的“魔力”。

udf udf

/*
1. All invisible TAB, Carriage Return, and Line Feed characters will be replaced with spaces.
2. Then leading and trailing spaces are removed from the value. 
3. Further, contiguous occurrences of more than one space will be replaced with a single space.
*/
CREATE FUNCTION dbo.udf_tokenize(@input VARCHAR(MAX))
   RETURNS VARCHAR(MAX)
AS
BEGIN 
   RETURN (SELECT CAST('<r><![CDATA[' + @input + ']]></r>' AS XML).value('(/r/text())[1] cast as xs:token?','VARCHAR(MAX)'));
END

Test harness测试工具

-- DDL and sample data population, start
DECLARE @mockTbl TABLE (ID INT IDENTITY(1,1), col_1 VARCHAR(100), col_2 VARCHAR(100));
INSERT INTO @mockTbl (col_1, col_2)
VALUES ('  FL   ', '  Miami')
   , ('  FL   ', '  Fort       Lauderdale   ')
   , ('  NY   ', '  New           York   ')
   , ('  NY   ', '')
   , ('  NY   ', NULL);
-- DDL and sample data population, end

SELECT * 
    , col_1n = dbo.udf_tokenize(col_1)
    , col_2n = dbo.udf_tokenize(col_2)
    , CONCAT_WS('-', dbo.udf_tokenize(col_1), dbo.udf_tokenize(col_2)) AS RESULT
FROM @mockTbl;

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

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