我正在嘗試創建一個查詢以從大字串中洗掉一些 varchar 和符號,基本上一個表將有一個具有這種格式的列(資訊來自 API 呼叫):
$.owner = "[email protected]" and $.asignee ="joe" and $.Entities.Entity = "12345" And $.CountryService.Country ="1" and $.CountryService.Service="B"
所以要求是從樣本中獲取主要的“列名”,所以最后的字串將是:
owner = "[email protected]" and asignee ="joe" and Entity = "12345" And Country ="1" and Service="B"
這應該是動態的,因為我們可以擁有更多資料,例如 $.Entities.Name、$.CountryService.Region 等
uj5u.com熱心網友回復:
這相當簡單,可以利用STRING_SPLIT、STRING_AGG和來完成CHARINDEX。
DECLARE @string VARCHAR(1000) =
'$.owner = "[email protected]" 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 = "[email protected]" and asignee ="joe" and Entity = "12345" and Country ="1" and Service="B"
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452573.html
上一篇:T-SQL和SSRS多引數
下一篇:C中生成解密密鑰的RSA演算法
