I'm trying to create an SQL function that returns comma separated value of all months that have the value true by reading the xmlNode.
However, I'm unable to navigate to the months node at all.
Adding namespace to the path hasn't helped me extract the value from Node.Data
For the first node (StartDateTime), I was able to extract the value on similar lines (by using the path as
/ScheduleDefinition/StartDateTime
Please refer the code below:
CREATE FUNCTION [dbo].[GetMonthsFromXML]
(
-- Add the parameters for the function here
@xmlText NTEXT
)
RETURNS VARCHAR(MAX)
AS
BEGIN
/*
<ScheduleDefinition>
<StartDateTime>04/10/2019 06:00:00</StartDateTime>
<MonthlyRecurrence xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/03/01/ReportServer">
<Days>29</Days>
<MonthsOfYear>
<January>true</January>
<February>false</February>
<March>true</March>
<April>true</April>
<May>false</May>
<June>false</June>
<July>false</July>
<August>false</August>
<September>false</September>
<October>false</October>
<November>false</November>
<December>false</December>
</MonthsOfYear>
</MonthlyRecurrence>
</ScheduleDefinition>
*/
DECLARE @resultVar VARCHAR(MAX)
DECLARE @x xml
SET @x = CAST(@xmlText AS XML);
;with cte
as
(
SELECT
[MonthName] = Node.Data.value('local-name(.)', 'VARCHAR(20)')
,MonthValue = Node.Data.value('(.)[1]', 'VARCHAR(20)')
FROM
@x.nodes('/ScheduleDefinition/MonthlyRecurrence/MonthsOfYear') Node(Data)
)
select @resultVar = ISNULL( MAX(MonthValue),'') from cte; --where MonthValue is not null and MonthValue <> '' ;
-- Return the result of the function
RETURN @resultVar
END
GO
Jan, Mar, Apr is the expected output for the above xml