[英]Pull the particular string from nvarchar type column in SQL Server
I have a string like &hprop=anprop_p&asofmonth=01/2017&OutputType=PDF&IsGrid=-&ReportCode=AllCol1&Attach=NO&IsRequestQue=true
and want to pull the values partitioned by & from string.我有一个类似&hprop=anprop_p&asofmonth=01/2017&OutputType=PDF&IsGrid=-&ReportCode=AllCol1&Attach=NO&IsRequestQue=true
的字符串,并且想从字符串中提取由 & 分区的值。
As we see above each string is separated with & and both the values have a name ie Outputtype=
and ReportCode=
正如我们在上面看到的,每个字符串都用 & 分隔,并且两个值都有一个名称,即Outputtype=
和ReportCode=
In SQL query it should return only values in different columns.在 SQL 查询中,它应该只返回不同列中的值。 AllCol1 Aand PDF AllCol1 A和PDF
I have tried the below query but it is pulling string ReportCode=AllCol1我已经尝试了以下查询,但它正在拉字符串 ReportCode=AllCol1
declare @Str varchar(500)
select SUBSTRING(SUBSTRING(@Str, CHARINDEX('&ReportCode=', @Str) + 1, LEN(@Str)), 0, CHARINDEX('&', SUBSTRING(@Str, CHARINDEX('&', @Str) +1, LEN(@Str))))
As you are using SQL Server 2016, you can take advantage of STRING_SPLIT()
to split your url into the component query parameters, eg当您使用 SQL Server 2016 时,您可以利用STRING_SPLIT()
将 url 拆分为组件查询参数,例如
SELECT *
FROM STRING_SPLIT(N'&hprop=anprop_p&asofmonth=01/2017&OutputType=PDF&IsGrid=-&ReportCode=AllCol1&Attach=NO&IsRequestQue=true', '&');
Will return:将返回:
value
-----------------
hprop=anprop_p
asofmonth=01/2017
OutputType=PDF
IsGrid=-
ReportCode=AllCol1
Attach=NO
IsRequestQue=true
You would then need to split each result on =
to separate it into the parameter name and the argument.然后,您需要将每个结果拆分为=
以将其分隔为参数名称和参数。 eg例如
SELECT s.value,
Parameter = CASE WHEN CHARINDEX('=', s.value) = 0 THEN s.value ELSE LEFT(s.value, CHARINDEX('=', s.value) - 1) END,
Value = CASE WHEN CHARINDEX('=', s.value) = 0 THEN NULL ELSE SUBSTRING(s.value, CHARINDEX('=', s.value) + 1, LEN(s.value)) END
FROM STRING_SPLIT(N'&hprop=anprop_p&asofmonth=01/2017&OutputType=PDF&IsGrid=-&ReportCode=AllCol1&Attach=NO&IsRequestQue=true', '&') s;
Returns:回报:
value Parameter Value
-------------------------------------------------
NULL
hprop=anprop_p hprop anprop_p
asofmonth=01/2017 asofmonth 01/2017
OutputType=PDF OutputType PDF
IsGrid=- IsGrid -
ReportCode=AllCol1 ReportCode AllCol1
Attach=NO Attach NO
IsRequestQue=true IsRequestQue true
Finally, you would just need to extract the terms you are actually interested in, and PIVOT them to bring back one row.最后,您只需要提取您真正感兴趣的术语,然后将它们 PIVOT 带回一行。 Bringing it all together, you get:把它们放在一起,你会得到:
DECLARE @T TABLE (ID INT IDENTITY, Col NVARCHAR(MAX));
INSERT @T (Col)
VALUES
(N'&hprop=anprop_p&asofmonth=01/2017&OutputType=PDF&IsGrid=-&ReportCode=AllCol1&Attach=NO&IsRequestQue=true'),
(N'&hprop=anprop_p&asofmonth=01/2017&OutputType=XLS&IsGrid=-&ReportCode=AllCol3&Attach=NO&IsRequestQue=false');
SELECT pvt.ID, pvt.OutputType, pvt.ReportCode
FROM ( SELECT T.ID,
t.Col,
Parameter = CASE WHEN CHARINDEX('=', s.value) = 0 THEN s.value ELSE LEFT(s.value, CHARINDEX('=', s.value) - 1) END,
Value = CASE WHEN CHARINDEX('=', s.value) = 0 THEN NULL ELSE SUBSTRING(s.value, CHARINDEX('=', s.value) + 1, LEN(s.value)) END
FROM @T AS t
CROSS APPLY STRING_SPLIT(T.Col, '&') AS s
WHERE s.value <> ''
) AS t
PIVOT (MAX(Value) FOR Parameter IN ([ReportCode], [OutputType])) AS pvt;
Which returns:返回:
ID OutputType ReportCode
----------------------------------
1 PDF AllCol1
2 XLS AllCol3
Use string_split()
:使用string_split()
:
select max(case when s.value like 'Outputtype=%'
then stuff(s.value, 1, 11, '')
end) as Outputtype,
max(case when s.value like 'ReportCode=%'
then stuff(s.value, 1, 11, '')
end) as ReportCode
from string_split(@str, '&') s;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.