sql - 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)
)
解决方案
欢迎来到 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), ' ', '')
该行应在这些列的连接字符串中将“”(空格)替换为“”(无)。
让我知道这是否回答了您的问题,或者我还能如何提供帮助。
推荐阅读
- java - springboot启动项目的pom.xml文件报错如何解决?
- sql - Postgres 中的递归 CTE
- php - PayPal IPN:读取多个 item_name 和数量并保存到数据库
- azure - Azure OAuth2 客户端凭据流 - 获取多个范围的令牌会引发错误
- node.js - WebSocket 连接到“wss://api.example.com/ws”失败:WebSocket 握手期间出错:意外响应代码:404
- gitkraken - GitKraken:无法打开公钥文件
- javascript - 如何在 javascript 中重定向自定义生成的 URL?
- android - Android 工作室 - MVP 模式
- ios - Siri,通过 Swift 改变语言识别,动态
- python - 在 Django 中的表单操作后无法重定向