We have a third party app which generates an XML file with data which is submitted from a mobile device; this comes back into a SQL Database Table and sits inside an XML type field.
I need to pull out specific answers which I seem to have figured out to a certain extend but I would like to know if I could make this work with the 'nodes' or some form of Outer Apply/Cross Apply Join to then query direct rather than a full line per value.
Examples below:
CREATE TABLE [dbo].[zz_PhoneData_Test](
[RecID] [bigint] NOT NULL,
[TasksetID] [bigint] NULL,
[FormData] [xml] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT [reports].[zz_PhoneData_Test] ([RecID],[TasksetID], [FormData]) VALUES (35512921264, 593, N'<fd u="=afa01113c0674a3fbdc8354c7aa538ab" b="0" v="1"><field i="1">12345</field><field i="2">E-mail Test</field><field i="3">1</field></fd>')
WITH [cte_test] AS
(
SELECT TOP(10)
[pd].[RecId],
REPLACE(REPLACE(CAST(FormData.query('/fd/field[@i=1]') AS NVARCHAR(100)),'<field i="1">',''),'</field>','') AS [Order]
FROM reports.[zz_PhoneData_Test] [pd]
)
SELECT [cte].[RecId],
[cte].[Order],
CASE WHEN PATINDEX('[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][-][0-9][0-9][0-9][0-9]', [Order] ) = 1 THEN 'Valid'
WHEN PATINDEX('[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]', [Order] ) = 1 THEN 'Valid'
WHEN PATINDEX('[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]', [Order] ) = 1 THEN 'Valid'
ELSE 'Invalid' END AS [Validation]
FROM [cte_test] [cte]
GO
With the above it should be enough to get you an example of where I am up to, ignore the PATINDEX as that can be left out of testing, I want to be able to pull the values for each of the 3 answers in the "XML" without using the 'query' option but via the 'nodes' method, although I'm not to sure on how to do this? Any help or guidance is appreciated.