In a sql query i run these commands and i keep getting NUll no matter what i try.
DECLARE @x AS XML
SET @x = ' <Data> <Preference>Mail</Preference> <Comment>Changed Contact Communication Preference due to customer unsubscribed</Comment> </Data> '
SELECT @x.value('(//Data/@Preference)[0]','varchar(20)') AS Preference
what am i doing wrong?
Use this XPath instead:
All XPath arrays are 1-based – not 0-based – therefore your
[0]won’t return anything.And secondly,
<Preference>is an XML element – not an attribute. The@Preferenceyou have is trying to select a XML attribute