Function STRING_SPLIT in SQL Server 2014
With a split function (there are many ways to do this prior to SQL Server 2016, here is one):
CREATE FUNCTION [dbo].[SplitString]
(
@List NVARCHAR(MAX),
@Delim VARCHAR(255)
)
RETURNS TABLE
AS
RETURN ( SELECT [Value] FROM
(
SELECT
[Value] = LTRIM(RTRIM(SUBSTRING(@List, [Number],
CHARINDEX(@Delim, @List + @Delim, [Number]) - [Number])))
FROM (SELECT Number = ROW_NUMBER() OVER (ORDER BY name)
FROM sys.all_objects) AS x
WHERE Number <= LEN(@List)
AND SUBSTRING(@Delim + @List, [Number], LEN(@Delim)) = @Delim
) AS y
);
The query becomes simpler than a bunch of charindex etc. and an unknown endpoint:
DECLARE @String VARCHAR(1024) = 'LogsID = "1" Value="1" Value="2" Value="3"';
;WITH x AS
(
SELECT value, p = PARSENAME(value,1)
FROM dbo.SplitString(@String, 'Value=')
),
l AS
(
SELECT LogsID = PARSENAME(LTRIM(SUBSTRING(value,CHARINDEX('=',value)+1,255)),1)
FROM x WHERE p IS NULL
)
SELECT l.LogsID,y.p FROM l
CROSS JOIN (SELECT p FROM x WHERE p IS NOT NULL) AS y;
The STRING_SPLIT function in SQL Server is not available before SQL Server 2016, you should have at least compatibility level 130 as per Microsoft's docs.
Visit this link Below you can find a summary of each version and its compatibility level
SQL Server Versions | Compatibility Level |
---|---|
SQL Server 2019 preview | 150 |
SQL Server 2017 (14.x) | 140 |
SQL Server 2016 (13.x) | 130 |
SQL Server 2014 (12.x) | 120 |
SQL Server 2012 (11.x) | 110 |
SQL Server 2008 R2 | 100 |
SQL Server 2008 | 100 |
SQL Server 2005 (9.x) | 90 |
SQL Server 2000 | 80 |