September 20, 2010 at 9:31 am
I have a large set of numbers in a list, but not in a table. Is it possible to use a SELECT statement to get a dataset with a single column of many rows?
Example:
SELECT 1, 2, 3, 4, 5
I'd like it to appear as:
1
2
3
4
5
Not:
1 | 2 | 3 | 4 | 5
Thanks in advance for any help that you're able to provide.
September 20, 2010 at 9:37 am
Take a gander at the first post of this thread: http://www.sqlservercentral.com/Forums/Topic988784-391-1.aspx
September 20, 2010 at 10:14 am
Thanks for your quick reply. I took a look and wasn't able to find a solution.
Were you referring to cross apply? I didn't think I could use that if I wasn't selecting the values from a table?
September 20, 2010 at 10:19 am
SJLee.Consulting (9/20/2010)
I have a large set of numbers in a list, but not in a table.
What exactly do you mean, "in a list, but not in a table"? Do you mean the numbers are in a single column value as a comma-delimited string, e.g. '1,2,3,4'?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 20, 2010 at 10:20 am
Ok, take this:
select ValueOfCols from
(
SELECT 1 col1, 2 col2, 3 col3, 4 col4, 5 col5
) Pivot_Table
UNPIVOT
(
ValueOfCols FOR Cols IN ([col1],[col2],[col3],[col4],[col5])
) Pivot_Hanle
September 20, 2010 at 10:24 am
If your input is as what Chris Morris said, then this would help you
DECLARE @CSV VARCHAR(100)
SELECT @CSV = '1,2,3,4,5'
SELECT x.i.value('(./text())[1]','INT') AS IDs
FROM (
SELECT XMLList=CAST('<i>'+REPLACE(@CSV,',','</i><i>')+'</i>' AS XML).query('.')
) a
CROSS APPLY
XMLList.nodes('i') x(i)
The above code has been discussed to an exhaustive extent in the thread link i said...
September 20, 2010 at 10:24 am
Yes, that's exactly it, here's a small portion:
SELECT 1502280,1506687,1504669,1501368,1500801,1500861,1501163,1521193,1500650,1500788,1512239,1500139,1502181,1500537,1500321,1506314,1502755,1505270,1500400,1515167,1505350,1509675,1500196,1502599,1501572,1501226,1517844,1503169,1512954
September 20, 2010 at 10:25 am
ColdCoffee (9/20/2010)
If your input is as what Chris Morris said, then this would help you
DECLARE @CSV VARCHAR(100)
SELECT @CSV = '1,2,3,4,5'
SELECT x.i.value('(./text())[1]','INT') AS IDs
FROM (
SELECT XMLList=CAST('<i>'+REPLACE(@CSV,',','</i><i>')+'</i>' AS XML).query('.')
) a
CROSS APPLY
XMLList.nodes('i') x(i)
This works perfectly, but the columns aren't named, I just have the word SELECT followed by 1000+ ints seperated by commas
September 20, 2010 at 10:30 am
Sorry, I quoted the wrong response last time, this worked perfectly, thank you very much.
I must have missed it in the link earlier, most likely because I don't quite understand it.
Thanks again for everyones help and quick responses.
September 20, 2010 at 10:38 am
Thanks for the feedback, Lee
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply