jeudi 13 août 2015

How to extract certain xml strings from a column in sql server 2008 and make a new table and columns using it's data?

Hi I have a table named 'BOOKING' and it has 4 columns, the 4th column's data came from a trigger which will have the xml string. Here's the sample data on the 4th column;

<i guest_id="PEK150700019-001" updated_by="sherwin" update_dt="2015-08-11T13:59:09.550" place_birth="SHANGHAI"/>

What I need is to extract the guest_id value, the updated_by vlaue, the update_dt value and so on: in other words, the xml string can have more strings in xml format.

I want to extract those strings from the 4th column and make a new table for it. Here is the sample output;

=================================================================
|guest_id          |   updated_by     |     update_dt           |
|==================|==================|=========================|
|PEK150700019-001  |  sherwin         |  2015-08-11T13:59:09.550|
|------------------|------------------|-------------------------|
|PEK150700019-002  |  wane            |  2015-09-11T13:00:09.540|
|==================|==================|=========================|

So Far, on my research I have done something like this;

  declare @txt varchar(max)
    set @txt ='<i guest_id="PEK150700019-001" updated_by="sherwin" update_dt="2015-08-11T13:59:09.550" place_birth="SHANGHAI"/>'

    SELECT  [updated_by] = SUBSTRING(audit_val,
    (CHARINDEX('updated_by',audit_val,1)+12),
    (CHARINDEX('"',audit_val,(CHARINDEX('updated_by',audit_val,1)+12)))-(CHARINDEX('updated_by',audit_val,1)+12)),''))

That code extracts the xml string 'updated_by' and making a new column for a table.

Can I ask for a help ? Is there a way to extract those xml strings and make a new columns for a new table on it ? I mean an easier way to do that without using substring or charindex like the one on my sample output? PLEASE HELP thanks in advance :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire