删除字符串和符号
Remove String and Symbols
我正在尝试创建一个查询以从一个大字符串中删除一些 varchar 和符号,基本上 table 将有一个具有这种格式的列(信息来自 API 调用) :
$.owner = "javier@tl.com" and $.asignee ="joe" and $.Entities.Entity = "12345" And $.CountryService.Country ="1" and $.CountryService.Service="B"
所以要求是从样本中获取主要的“列名”,所以最后的字符串将是这样的:
owner = "javier@tl.com" and asignee ="joe" and Entity = "12345" And Country ="1" and Service="B"
这应该是动态的,因为我们可以获得更多数据,例如 $.Entities.Name、$.CountryService.Region 等
这很简单,可以利用 STRING_SPLIT
、STRING_AGG
和 CHARINDEX
.
来完成
DECLARE @string VARCHAR(1000) =
'$.owner = "javier@tl.com" and $.asignee ="joe" and $.Entities.Entity = "12345" And $.CountryService.Country ="1" and $.CountryService.Service="B"';
SELECT NewString =
STRING_AGG(SUBSTRING(split.value,IIF(p.P1>0 AND p.P2>p.P1,p.P1+1,1),8000),'and ')
FROM STRING_SPLIT(REPLACE(REPLACE(@string,'$.',''),'and ','|'),'|') AS split
CROSS APPLY (VALUES(CHARINDEX('.',split.value), CHARINDEX('"',split.value))) AS p(P1,P2);
结果:
owner = "javier@tl.com" and asignee ="joe" and Entity = "12345" and Country ="1" and Service="B"
我正在尝试创建一个查询以从一个大字符串中删除一些 varchar 和符号,基本上 table 将有一个具有这种格式的列(信息来自 API 调用) :
$.owner = "javier@tl.com" and $.asignee ="joe" and $.Entities.Entity = "12345" And $.CountryService.Country ="1" and $.CountryService.Service="B"
所以要求是从样本中获取主要的“列名”,所以最后的字符串将是这样的:
owner = "javier@tl.com" and asignee ="joe" and Entity = "12345" And Country ="1" and Service="B"
这应该是动态的,因为我们可以获得更多数据,例如 $.Entities.Name、$.CountryService.Region 等
这很简单,可以利用 STRING_SPLIT
、STRING_AGG
和 CHARINDEX
.
DECLARE @string VARCHAR(1000) =
'$.owner = "javier@tl.com" and $.asignee ="joe" and $.Entities.Entity = "12345" And $.CountryService.Country ="1" and $.CountryService.Service="B"';
SELECT NewString =
STRING_AGG(SUBSTRING(split.value,IIF(p.P1>0 AND p.P2>p.P1,p.P1+1,1),8000),'and ')
FROM STRING_SPLIT(REPLACE(REPLACE(@string,'$.',''),'and ','|'),'|') AS split
CROSS APPLY (VALUES(CHARINDEX('.',split.value), CHARINDEX('"',split.value))) AS p(P1,P2);
结果:
owner = "javier@tl.com" and asignee ="joe" and Entity = "12345" and Country ="1" and Service="B"