Hi @Mike ,
Please also refer below method and check whether it is working.
Step 1: create one function SplitString.
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
);
Step 2:
;with cte as (
select cast(date1 as date) date1, [value]
from #Longdata
cross apply dbo.[SplitString](OperationDetail,'<')
)
,cte2 as (
select date1,case when CHARINDEX('>',value)>0 then left(value,CHARINDEX('>',value)-1) end val1
,case when CHARINDEX('>',value)>0 then SUBSTRING(value,CHARINDEX('>',value)+1,LEN(value)-CHARINDEX('>',value)) end val2
from cte )
select * from
(select * from cte2) a
pivot
(max(val2) for val1 in (Name,useridNo,username,EnvCode,LogCode)) b
Output:
date1 Name useridNo username EnvCode LogCode
2019-10-11 AAA 3929 Rob BB_HRD -10
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.