XQuery and syntax of either query or value method to pull data from xml column

  • What should the proper syntax be to pull the data back where value2 is yo5?

    SELECT AdditionalData.value('

    declare namespace C="test";

    (/C:DocumentElement/C:AdditionalData/C:Value2)[1]', 'VARCHAR(12)') AS answer

    from table

    I am trying to pull data from an xml data type column. The column when double clicked on looks like:

    <DocumentElement>

    <AdditionalData>

    <AdditionalDataID>1</AdditionalDataID>

    <DataCategoryID>1a</DataCategoryID>

    <Value1>yo1</Value1>

    <Value2>yo2</Value2>

    <Value3>yo3</Value3>

    </AdditionalData>

    <AdditionalData>

    <AdditionalDataID>2</AdditionalDataID>

    <DataCategoryID>2a</DataCategoryID>

    <Value1>yo4</Value1>

    <Value2>yo5</Value2>

    <Value3>yo6</Value3>

    </AdditionalData>

    </DocumentElement>

  • Barret-330682 (6/27/2011)


    What should the proper syntax be to pull the data back where value2 is yo5?

    SELECT AdditionalData.value('

    declare namespace C="test";

    (/C:DocumentElement/C:AdditionalData/C:Value2)[1]', 'VARCHAR(12)') AS answer

    from table

    I am trying to pull data from an xml data type column. The column when double clicked on looks like:

    <DocumentElement>

    <AdditionalData>

    <AdditionalDataID>1</AdditionalDataID>

    <DataCategoryID>1a</DataCategoryID>

    <Value1>yo1</Value1>

    <Value2>yo2</Value2>

    <Value3>yo3</Value3>

    </AdditionalData>

    <AdditionalData>

    <AdditionalDataID>2</AdditionalDataID>

    <DataCategoryID>2a</DataCategoryID>

    <Value1>yo4</Value1>

    <Value2>yo5</Value2>

    <Value3>yo6</Value3>

    </AdditionalData>

    </DocumentElement>

    Not sure what you mean by "the data". See if this gets you closer. If not, please clarify.

    DECLARE @xml XML = N'

    <DocumentElement>

    <AdditionalData>

    <AdditionalDataID>1</AdditionalDataID>

    <DataCategoryID>1a</DataCategoryID>

    <Value1>yo1</Value1>

    <Value2>yo2</Value2>

    <Value3>yo3</Value3>

    </AdditionalData>

    <AdditionalData>

    <AdditionalDataID>2</AdditionalDataID>

    <DataCategoryID>2a</DataCategoryID>

    <Value1>yo4</Value1>

    <Value2>yo5</Value2>

    <Value3>yo6</Value3>

    </AdditionalData>

    </DocumentElement>' ;

    SELECT @xml.query('for $ep in (/DocumentElement/AdditionalData)

    where $ep/Value2[1] eq "yo5"

    return <Values>

    { ($ep/Value2) }

    </Values>') ;

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • thanks

  • You're welcome 🙂

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply