The problem with the data is that it doesn't contain well formed xml
- you will need to wrap it before you can use the xml tools in Sql like xquery
.
SELECT *
FROM
(
SELECT
Nodes.node.value('(./@id)[1]', 'int') AS EmployeeId,
Nodes.node.value('(./@name)[1]', 'varchar(50)') AS EmployeeName
FROM
(
SELECT CAST('<xml>' + value + '</xml>' AS Xml) As WrappedXml
FROM TableA
) AS x
cross apply x.WrappedXml.nodes('//Employee') as Nodes(node)
) as y
WHERE
y.EmployeeId = 1;
- Inner select -wraps the xml
- Middle select - standard xquery
- Outer select - where filter
You haven't clarified what you mean w.r.t. get only record 1
, but if you mean just the first element of each row (which coincidentally also has id = 1), you can use ROW_NUMBER()
to assign a sequence:
SELECT *
FROM
(
SELECT
Nodes.node.value('(./@id)[1]', 'int') AS EmployeeId,
Nodes.node.value('(./@name)[1]', 'varchar(50)') AS EmployeeName,
ROW_NUMBER() OVER (PARTITION BY x.Id ORDER BY ( SELECT 1 )) SequenceId
FROM
(
SELECT Id, CAST('<xml>' + value + '</xml>' AS Xml) As WrappedXml
FROM TableA
) AS x
cross apply x.WrappedXml.nodes('//Employee') as Nodes(node)
) as y
WHERE SequenceId = 1;