Solved

SQL XML Select:  Need to get the index of the node

Posted on 2013-11-21
2
408 Views
Last Modified: 2013-11-22
OK

I am using this as a split function and need to insert the split into a table.

I don't want to put the results into a temp table if I don't have to.

DECLARE @value varchar(50)
SET @value = '9|5|22|298'
      
SET @xml = N'<root><r>' + replace(@value,'|','</r><r>') + '</r></root>'
SELECT
    --I NEED THE INDEX OF THE NODE HERE
      r.value('.','varchar(50)')
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)')) >0

--I Want to do something like this

INSERT INTO TABLE (order, valueID)
SELECT
    --I NEED THE INDEX OF THE NODE HERE
      r.value('.','varchar(50)')
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)')) >0

Thanks for the help
0
Comment
Question by:skillilea
2 Comments
 
LVL 12

Accepted Solution

by:
Saurabh Bhadauria earned 500 total points
ID: 39668164
Do it like this..
DECLARE @value varchar(50)
DECLARE @xml XML 
SET @value = '9|5|22|298'

      
SET @xml = N'<root><r>' + replace(@value,'|','</r><r>') + '</r></root>'
SELECT 
    ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS seq,
      r.value('.','varchar(50)') 
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)')) >0

--I Want to do something like this

INSERT INTO TABLE (order, valueID)
SELECT 
    ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS seq,
      r.value('.','varchar(50)') 
FROM @xml.nodes('//root/r') as records(r)
WHERE LEN(r.value('.','varchar(50)'))

Open in new window



Thanks,
Saurabh
0
 

Author Closing Comment

by:skillilea
ID: 39668386
Awesome!

Just what I needed...tnx tons
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question