Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query to select records from Xml column


Query to select records from Xml column

Author
Message
Paresh Dehadray
Paresh Dehadray
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 28
Hello Sir/Madam

I have following Xml in Job table which column name is JobXml (Xml datatype)

<Job>
<TimeID Value="2323"/>
<TimeID Value="4445"/>
<TimeID Value="3453"/>
<TimeID Value="7677"/>
</Job>

I want to write a select query who returns all TimeID Value like this

2323
4445
3453
7677

Please give me the query to select these records.
Currently I am using following query but it returns only one TimeID value:

select JobXml.value('(/Job/TimeID/@Value)[1]','int') from Job


Please help me.
Thanks in advance :-)
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2351 Visits: 23160
SELECT r.value('@Value','int')
FROM Job
CROSS APPLY JobXml.nodes('/Job/TimeID') AS x(r)



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Paresh Dehadray
Paresh Dehadray
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 28
Thank you very much. It works. What a nice query!!!!
You are great!!! (champs of SQL Server) :-)
avnish26
avnish26
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 97
Hello Expert,

Same above scenario i have...i have a table in which one column xmldata(having datatype xml) contain data in xml format, as given below:

<Employee>
<EMPID>1</EMPID>
<EMPNAME>AAA</EMPNAME>
<EMPSALARY>2000.00</EMPSALARY>
<DEPTID>1</DEPTID>
</Employee>

Now i have to read this xml data and display it with respective column names..like
EMPID EMPNAME EMPSALARY DEPTID
1 AAA 2000.00 1


please help..

Thanks in advance...

reagrds,
Avi
Florian Reischl
Florian Reischl
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2247 Visits: 3934
Hi Avi

To get the elements out of your XML you can use this:
DECLARE @xml XML

SELECT @xml = N'<Employee>
<EMPID>1</EMPID>
<EMPNAME>AAA</EMPNAME>
<EMPSALARY>2000.00</EMPSALARY>
<DEPTID>1</DEPTID>
</Employee>'

SELECT
T.C.value('(EMPID)[1]', 'int')
,T.C.value('(EMPNAME)[1]', 'nvarchar(20)')
,T.C.value('(EMPSALARY)[1]', 'decimal(15,5)')
,T.C.value('(DEPTID)[1]', 'int')
FROM @xml.nodes('Employee') T(C)



If you have to apply this to your table, have a look for CROSS APPLY with XML.

Greets
Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
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