SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extracting XML


Extracting XML

Author
Message
timvil
timvil
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 6
I know this is not a new issue but:
How can I extract data from XML column:
I have DB1 and Table1 and Column1. In Column1 I have XML data: <instance><Title>New product</Title> <Date> 2013.10.24</Date><Author>TV</Author><Meter>LV</Meter><Result>90</ Result ></instance>
Now I want to query two values from that column: <Title> and <Result> to make an diagram.
How can I do this?
mister.magoo
mister.magoo
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4110 Visits: 7865

declare @Table1 table(Column1 xml);

insert @Table1(Column1)
values( '<instance><Title>New product</Title> <Date> 2013.10.24</Date><Author>TV</Author><Meter>LV</Meter><Result>90</Result ></instance>');

select
Column1.value('(instance/Title/text())[1]','varchar(255)') as title
, Column1.value('(instance/Result/text())[1]','varchar(255)') as result
from @Table1



MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • timvil
    timvil
    Forum Newbie
    Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

    Group: General Forum Members
    Points: 2 Visits: 6
    Thanks MM for your fast reply!
    However I didn’t get it to work. Maybe problem is in Column1 data type, it is ntext. I read somewhere that it couldn’t be ntext type in declare procedure. Is it so?
    I have another way to do it: just have to merge two rows because now it is like that: (Ids are same in two pair rows)
    Id key value
    41 Title text1
    41 Result 90
    42 Title text2
    42 Result 150 etc.
    and I would like to get it to form:
    Id key value key2 value2
    41 Title text1 Result 90
    42 Title text2 Result 150 etc.
    Do you have any ideas for this.
    Alan.B
    Alan.B
    SSCertifiable
    SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

    Group: General Forum Members
    Points: 5121 Visits: 7703
    timvil (11/11/2013)
    Thanks MM for your fast reply!
    However I didn’t get it to work. Maybe problem is in Column1 data type, it is ntext. I read somewhere that it couldn’t be ntext type in declare procedure. Is it so?


    You cannot declare a variable as ntext. If column1 is ntext, to get it working using mm's code you would do this:


    declare @Table1 table(Column1 ntext);

    insert @Table1(Column1)
    values( '<instance><Title>New product</Title> <Date> 2013.10.24</Date><Author>TV</Author><Meter>LV</Meter><Result>90</Result ></instance>');

    WITH ntext_to_xml(Column1) AS (SELECT CAST(Column1 AS xml) FROM @Table1)
    select
    Column1.value('(instance/Title/text())[1]','varchar(255)') as title
    , Column1.value('(instance/Result/text())[1]','varchar(255)') as result
    from ntext_to_xml



    I have another way to do it: just have to merge two rows because now it is like that: (Ids are same in two pair rows)
    Id key value
    41 Title text1
    41 Result 90
    42 Title text2
    42 Result 150 etc.
    and I would like to get it to form:
    Id key value key2 value2
    41 Title text1 Result 90
    42 Title text2 Result 150 etc.
    Do you have any ideas for this.


    This looks simple but do you have any sample data? This information does not exist in the sample data that you provided...

    -- Alan Burstein



    Best practices for getting help on SQLServerCentral
    Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
    Need a pattern-based splitter? Try PatternSplitCM
    Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

    "I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search