我在一栏中有以下数据:
main.cpp
如何选择所有分开的数据?
从上面的例子我需要以下结果:
a:5:{i:1;s:1:"2";i:2;s:39:"Customer Name";i:3;s:41:"Occupation";i:4;s:9:"Extra Data";i:5;s:16:"Extra data";}
提前致谢,
Sakis
答案 0 :(得分:2)
您可以使用XML转换:
DECLARE @str nvarchar(max) = 'a:5:{i:1;s:1:"2";i:2;s:39:"Customer Name";i:3;s:41:"Occupation";i:4;s:9:"Extra Data";i:5;s:16:"Extra data";}',
@x xml
SELECT @x = CAST('<n>'+REPLACE(REPLACE(@str,':"','</n><a>'),'";','</a><n>')+'</n>' as xml)
SELECT t.c.value('a[1]','nvarchar(max)') as [Data1],
t.c.value('a[2]','nvarchar(max)') as [Data2],
t.c.value('a[3]','nvarchar(max)') as [Data3],
t.c.value('a[4]','nvarchar(max)') as [Data4],
t.c.value('a[5]','nvarchar(max)') as [Data5]
FROM @x.nodes('/') as t(c)
输出:
Data1 Data2 Data3 Data4 Data5
----- ------------- ---------- ---------- ----------
2 Customer Name Occupation Extra Data Extra data
(1 row(s) returned)
转换为XML后,你会得到这个:
<n>a:5:{i:1;s:1</n>
<a>2</a>
<n>i:2;s:39</n>
<a>Customer Name</a>
<n>i:3;s:41</n>
<a>Occupation</a>
<n>i:4;s:9</n>
<a>Extra Data</a>
<n>i:5;s:16</n>
<a>Extra data</a>
<n>}</n>
答案 1 :(得分:0)
这比XML方法的效率要高得多,执行计划要简单得多:
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_ColumnChart" class="chart"></div>
请注意,只有字符串中有10个引号,这两种方法才有效。如果有更多报价,两者都会有效。