1
table tbl_event_log

columnName DataType
id          Int
event       XML
userinfo    XML

and data should be in event is

<Event><Player readName="9.Make You Talk!" demoName="Video Game" **portal="FB"** totalDuration="0:07/0:07(100%)" /></Event>

and i want to write query to get data from portal="FB"

2
  • Can you give better explanation as what you need and how your XML looks like? Your expected output? Commented Nov 28, 2016 at 6:05
  • expected output is like this id userInfo event 1204462 null <Event><Player readName="9.Make You Talk!" demoName="Video Game" portal="FB" totalDuration="0:07/0:07(100%)" /></Event> Commented Nov 28, 2016 at 6:12

2 Answers 2

2

Use nodes() method to split your rows and then get values: Check this solution and hope it helps you:

Declare @mytable table (id int,event xml,userinfo varchar(200))

Insert into @mytable
select 1, '<Event><Player readName="9.Make You Talk!" demoName="Video Game" portal="FB" totalDuration="0:07/0:07(100%)" /></Event>','Test'
Union
select 2, '<Event><Player readName="9.Make You Talk!" demoName="Video Game" portal="TW" totalDuration="0:07/0:07(100%)" /></Event>','Test'



select
    s.id,
    e.p.value('@portal', 'varchar(max)') as portal
from @mytable as s
    outer apply s.event.nodes('Event/Player') as e(p)

select * from (
select
    s.id,
    e.p.value('@portal', 'varchar(max)') as portal
from @mytable as s
    outer apply s.event.nodes('Event/Player') as e(p)
) Test
where Test.portal = 'FB'

Attn: Replace @mytable with your table name.

Note: Event column in your table must be XML datatype.

Sign up to request clarification or add additional context in comments.

Comments

0

To get the data into specific fields you could extract them directly from the xml.

For example:

select id, userinfo,
event.value('/Event[1]/Player[@portal="FB"][1]/@readName','varchar(max)') as readNameFB,
event.value('/Event[1]/Player[@portal="FB"][1]/@demoName','varchar(max)') as demoNameFB,
event.value('/Event[1]/Player[@portal="FB"][1]/@totalDuration','varchar(30)') as totalDurationFB
from tbl_event_log;

[@portal="FB"] : only get the Player tags where the portal="FB"
[1] : use the first one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.