我在尝试拆分存储在SQL表字段中的数组字符串时遇到了一些麻烦" SQL Server 2014"。
我的数据看起来像
array(
"status"=>array("pre"=>"2","aft"=>1)
"tier_ppl"=>array("pre"=>5.00,"aft"=>5.00)
"tier_commission"=>array("pre"=>5.00,"aft"=>500.00)
"tier_commission_datetime"=>array("pre"=>"2017-02-10 12:30:59","aft"=>"2017-02-14 08:54:32")
"affiliate_commission"=>array("pre"=>4.00,"aft"=>4.00)
"affiliate_commission_datetime"=>array("pre"=>"2017-02-10 12:30:59","aft"=>"2017-02-10 12:30:59")
)
我需要让我的外观看起来像: -
arrayname pre aft
status 5.00 5.00
tier_ppl 5.00 500.00
tier_commission 5.00 500.00
tier_commission_datetime 2017-02-10 12:30:59 2017-02-14 08:54:32
affiliate_commission 4.00 4.00
affiliate_commission_datetime 2017-02-10 12:30:59 2017-02-10 12:30:59
我拥有的每个记录都有一个这样的数组,尽管有时候前或后都是空白的。我尝试过拆分功能路线,但是我无法按照我想要的方式看待我的数据,有人做过这样的事情之前可以帮忙。
谢谢
答案 0 :(得分:0)
除此之外,因为我在工作中感到无聊,所以我决定告诉你,在SQL中尝试这样做是多么愚蠢。以下内容适用于您的示例数据,但在很多pre
和aft
值上可能会失败。例如,如果您的数据中有charindex
或patindex
个匹配模式。
利用Jeff Moden's string splitting function,您可以执行以下操作:
declare @a nvarchar(1000) =
'array(
"status"=>array("pre"=>"2","aft"=>1)
"tier_ppl"=>array("pre"=>5.00,"aft"=>5.00)
"tier_commission"=>array("pre"=>5.00,"aft"=>500.00)
"tier_commission_datetime"=>array("pre"=>"2017-02-10 12:30:59","aft"=>"2017-02-14 08:54:32")
"affiliate_commission"=>array("pre"=>4.00,"aft"=>4.00)
"affiliate_commission_datetime"=>array("pre"=>"2017-02-10 12:30:59","aft"=>"2017-02-10 12:30:59")
)';
select replace(left(s.Item,charindex('"=',s.Item,1)),'"','') as arrayname
,replace(replace(substring(s.Item,charindex('"pre"=>',s.Item,1),patindex('%,"%',s.Item) - charindex('"pre"=>',s.Item,1)),'"pre"=>',''),'"','') as Pre
,reverse(replace(replace(left(reverse(s.Item),charindex('>=',reverse(s.Item),1)-1),')',''),'"','')) as Aft
,s.Item
from dbo.DelimitedSplit8K(@a,char(10)) s
where left(Item,1) = '"';
将输出:
+-------------------------------+---------------------+----------------------+----------------------------------------------------------------------------------------------------+
| arrayname | Pre | Aft | Item |
+-------------------------------+---------------------+----------------------+----------------------------------------------------------------------------------------------------+
| status | 2 | 1 | "status"=>array("pre"=>"2","aft"=>1) |
| tier_ppl | 5.00 | 5.00 | "tier_ppl"=>array("pre"=>5.00,"aft"=>5.00) |
| tier_commission | 5.00 | 500.00 | "tier_commission"=>array("pre"=>5.00,"aft"=>500.00) |
| tier_commission_datetime | 2017-02-10 12:30:59 | 2017-02-14 08:54:32 | "tier_commission_datetime"=>array("pre"=>"2017-02-10 12:30:59","aft"=>"2017-02-14 08:54:32") |
| affiliate_commission | 4.00 | 4.00 | "affiliate_commission"=>array("pre"=>4.00,"aft"=>4.00) |
| affiliate_commission_datetime | 2017-02-10 12:30:59 | 2017-02-10 12:30:59 | "affiliate_commission_datetime"=>array("pre"=>"2017-02-10 12:30:59","aft"=>"2017-02-10 12:30:59") |
+-------------------------------+---------------------+----------------------+----------------------------------------------------------------------------------------------------+