首页 > 解决方案 > SQL 连接字符串,然后删除空格

问题描述

我正在尝试解决如何连接多个字段,删除它们之间的任何空格,然后将它们与另一个值进行比较。

我有以下字段:地址 1、城镇、县和邮政编码。基本上我需要将这些字符串连接起来,然后删除任何空格等。有人可以告诉我如何在我的以下 sql 中执行此操作:

select distinct p.GtId,
    p.CrmPartyId,
    p.LegalName,
    p.BusinessClass,
    p.RmFullName,
    p.PbeFullName,
    p.OverallClientStatus,
    p.OverallRpStatus,
   a.AddressType
from CORE.WeccoParty p
    join CORE.WeccoPartyAddress a ON p.GtId = a.GtId
where exists (select 1
           from CORE.WeccoParty sub_p
           left join CORE.WeccoPartyAddress sub_a 
             on sub_p.GtId = sub_a.GtId 
           where (p.FirstName     =  sub_p.FirstName
                  and p.LastName  =  sub_p.LastName
                  and a.Address1=  sub_a.Address1
                  and a.Town = sub_a.Town
                  and a.County = sub_a.County
                  and a.Postcode = sub_a.Postcode
                  and a.GtId           <> sub_a.GtId)
          )

标签: sqlsql-serverstring

解决方案


欢迎来到 StackOverflow!从您的标签看来,您正在使用 SQL 服务器。假设您是,您可以使用以下两个函数来帮助实现您的目标:

REPLACE:https
: //www.techonthenet.com/sql_server/functions/replace.php CONCAT:https ://www.techonthenet.com/sql_server /functions/concat.php

REPLACE 函数将简单地用 '' 表示的空白替换一个空白,用 '' 表示。
然后 CONCAT 函数将它们连接在一起。

如果我正在查看您的 SQL 代码并尝试根据您的描述连接 Address1、Town、County 和 Postcode,并保留您当前拥有的所有内容,我可能会执行以下操作:

select distinct p.GtId,
    p.CrmPartyId,
    p.LegalName,
    p.BusinessClass,
    p.RmFullName,
    p.PbeFullName,
    p.OverallClientStatus,
    p.OverallRpStatus,
   a.AddressType,
   REPLACE(CONCAT(a.Address1, a.Town, a.County, a.Postcode), ' ', '')
from CORE.WeccoParty p
    join CORE.WeccoPartyAddress a ON p.GtId = a.GtId
where exists (select 1
           from CORE.WeccoParty sub_p
           left join CORE.WeccoPartyAddress sub_a 
             on sub_p.GtId = sub_a.GtId 
           where (p.FirstName     =  sub_p.FirstName
                  and p.LastName  =  sub_p.LastName
                  and a.Address1=  sub_a.Address1
                  and a.Town = sub_a.Town
                  and a.County = sub_a.County
                  and a.Postcode = sub_a.Postcode
                  and a.GtId           <> sub_a.GtId)
          )

我添加的行是:
REPLACE(CONCAT(a.Address1, a.Town, a.County, a.Postcode), ' ', '')

该行应在这些列的连接字符串中将“”(空格)替换为“”(无)。

让我知道这是否回答了您的问题,或者我还能如何提供帮助。


推荐阅读