Subscribe To

Subscribe to SQL Server and XML by Email
Labels

Thursday, June 26, 2008

XQuery Lab 1 - Transforming rows to columns

This post is moved to http://beyondrelational.com/blogs/jacob/archive/2008/06/26/xquery-lab-1-transforming-rows-to-columns.aspx

5 comments:

  1. Hello, im new at xquery, so im little lost, my xml file have a different structure... so i would love if u can give me a little help...

    < imovel >
    < referencia >48428< /referencia >
    < tipo id="1" / >
    < tipologia id="3" / >
    < estado>NOVO< /estado >
    < distrito id="14" / >
    < concelho id="1416" / >
    < freguesia id="141621" / >
    < /imovel >
    As u can see my xml dont have the "value" as ur example have...

    so how it would be my select ?

    this is your example

    SELECT
    x.value('(Field[@Name="CONFIG_ID"]/@Value)[1]','VARCHAR(20)') AS CONFIG_ID,
    x.value('(Field[@Name="INST_ID"]/@Value)[1]','INT') AS INST_ID,
    x.value('(Field[@Name="CHARC"]/@Value)[1]','VARCHAR(20)') AS CHARC,
    x.value('(Field[@Name="VALUE"]/@Value)[1]','INT') AS VALUE
    FROM @x.nodes('/QUOTE/CSTICS/ORDER_CFGS_VALUE') d(x)


    Best Regards

    ReplyDelete
  2. Sure, I can help you.
    What is the output that you are expecting from this SAMPLE xml document?

    ReplyDelete
  3. hello again, and thanks for answer so quick...

    Lets see if my english can help my explanation :)

    In the next example i want three columns, the first "Referencia" , the select returns the value i want, in the second "tipologia" the select return "null" (dont know why ), and in the third "para_venda", the select return zero to this column, because dont have any value, what i would like to this column is, when appear on xml returns 1 if not returns 0 or null

    DECLARE @x XML
    SELECT @x = '< imovel >
    < referencia >48428< /referencia >
    < tipologia id="3" />
    < para_venda / >
    < /imovel >'

    SELECT
    x.value('referencia[1]','INT') AS referencia, -- WORKS PERFECT
    x.value('tipologia[1]','INT') AS tipologia, --SHOW ME NULL ON SELECT
    x.value('para_venda[1]','BIT') AS para_venda -- I want to return 1
    FROM @x.nodes('/imovel') d(x)

    Thanks for ur help me again...

    :)

    ReplyDelete
  4. Try this:

    DECLARE @x XML
    SELECT @x = '
    < imovel >
    < referencia >48428< /referencia >
    < tipologia id="3"/ >
    < para_venda / >
    < /imovel >'

    SELECT
    x.value('referencia[1]','INT') AS referencia,
    x.value('(tipologia/@id)[1]','INT') AS tipologia,
    CASE
    WHEN x.exist('para_venda') = 1 THEN 1
    ELSE 0
    END AS para_venda
    FROM @x.nodes('/imovel') d(x)

    /*
    OUTPUT:

    referencia tipologia para_venda
    ----------- ----------- -----------
    48428 3 1
    */

    ReplyDelete
  5. Thanks Thanks Thanks, it workssss,

    You're the Man:)

    ReplyDelete

About Me
Jacob Sebastian
Tutorials
* XQuery Tutorials
My Links
SQL Server White Papers
SQL Server 2008
My Articles
XML Workshop RSS Feed
Contact me
Readers
Free Hit Counter
Web Site Hit Counters
SQL Server Bloggers
Blog Directories
blogarama - the blog directory Programming Blogs - BlogCatalog Blog Directory
 
Copyright Jacob Sebastian