Solved

sql select from list syntax help

Posted on 2009-05-06
5
981 Views
Last Modified: 2012-05-06
I want to do a select

where I have a comma separated list

select
(1),
(2)

select
1,2,3

but these keep selecting into new columns
how do I write it so it selects each one into a new row

like
- no column name
1 1
2 2
3 3

0
Comment
Question by:pmtolk1
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
brad2575 earned 125 total points
ID: 24314110
You would have to do something like this:

select (1)

UNION

select (2)

UNION

select (3)
0
 
LVL 2

Author Comment

by:pmtolk1
ID: 24314327
I know you can do it as a list I just forgot the syntax
I am not crazy I have done this before damn cinco de mayo


If I have a long list
1,2,3,4,5,6..n

I should be able write

Select ((1,2,3,4,5,6..n)) somehow to get it to select as Rows right?
0
 
LVL 25

Expert Comment

by:reb73
ID: 24314427
You can do it with a UDF (User Defined Function) or a CTE (if on MS SQL Server 2005 or above).. What version of MS SQL Server is your backend?
0
 
LVL 25

Assisted Solution

by:reb73
reb73 earned 125 total points
ID: 24314511
Brandon Galderisi's post in the link (fn_DelimitedToTable) below should do the trick for you -

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_23774477.html#a22605889
0
 
LVL 2

Author Comment

by:pmtolk1
ID: 24314645
I remember the syntax now

SELECT [ID]
  FROM [DB].[dbo].[oops]
where ID in
(
1,2,3
)

which of course isnt what I wanted so I guess that means you guys are right?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2 IIF's in Access query 25 44
SQL Server group with two values 4 35
table joins in qry 17 65
Requesting help with creating an SQL query to select similar records 4 15
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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