Re: XQuery - Only return if not null



Hi Martin!

Thank you so much! Thats what I need!
Thank you , thank you!

"Martin Honnen" <mahotrash@xxxxxxxx> schrieb im Newsbeitrag news:uG9d44hhJHA.1248@xxxxxxxxxxxxxxxxxxxxxxx
Mike Müller wrote:

The problem is that my XML document does sometimes contatin the ElementABC and sometimes the ElementDEF. So if the ElelmetABC is found, then i want to use it's P1 value for the ColumnA. If it is not there the i want to try if the ElementDEF is found and use it's value for ColumnA.

Here is an example:

DECLARE @x XML;
SET @x = N'<Root>
<ElementABC P1="07" P2="07"/>
<ElementDEF P1="09" P2="07" P4="43" />
<ElementGHI P1="01" P2="07" P8="43" />
</Root>';

SELECT @x.value('(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)[1]', 'nvarchar(5)');

SET @x = N'<Root>
<ElementDEF P1="09" P2="07" P4="43" />
<ElementGHI P1="01" P2="07" P8="43" />
</Root>';

SELECT @x.value('(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)[1]', 'nvarchar(5)');

So you simply use a sequence
(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)
and then access its first item with
(/Root/ElementABC[1]/@P1, /Root/ElementDEF[1]/@P1)[1]
that way if ElementABC/@P1 exists, it is the first item in the sequence, if not, then ElementDEF/@P1 is the first item.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

.



Relevant Pages

  • Re: XQuery - Only return if not null
    ... If it is not there the i want to try if the ElementDEF is found and use it's value for ColumnA. ... that way if ElementABC/@P1 exists, it is the first item in the sequence, if not, then ElementDEF/@P1 is the first item. ... Martin Honnen --- MVP XML ...
    (microsoft.public.sqlserver.xml)
  • XQuery - Only return if not null
    ... Now I want to transpose the value of the P1 parameter into a new column named ColumnA. ... The problem is that my XML document does sometimes contatin the ElementABC and sometimes the ElementDEF. ...
    (microsoft.public.sqlserver.xml)

Loading