7

I can give a result set consisting of a single value, say 1, as follows:

SELECT 1 as column;

and it gives me the result set:

column
------
  1

But I have a list of such values represented as a string (1, 4, 7, ...) and I need to produce the following result set:

column
------
  1
  4
  7
  .
  .
  .

I tried SELECT * FROM (1, 4, 7) but it didn't work. I also tried to SELECT 1, 4, 7 but it produces the following result set:

col1   col2    col3
 1       4      7

Which was not what I was looking for.

1
  • You need to look at the String split or union Commented Mar 4, 2015 at 7:21

3 Answers 3

13

If those are constant values, you can use the values clause:

select * 
from (
   values (1), (4), (7)
) as t(id);

If your values are inside a string literal, you can use this:

select *
from unnest(string_to_array('1,2,3,4', ',')) as id;
Sign up to request clarification or add additional context in comments.

Comments

5

You could unnest it as an array:

SELECT UNNEST(ARRAY[1, 4, 7])

Comments

3

You can use the union To get what you want.But if this is the sting as 1,4,7 comma seprated then you need to use the regexp_split_to_table function. Mentioned here and here

Select 1
UNION
select 4
UNION
select 7

1 Comment

Your links are for SQL Server. Postgres already has everything built in to split a string into an array or even create a table of it.

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.