Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:25:12+00:00 2026-05-27T18:25:12+00:00

I have comma separated data in a column: Column ——- a,b,c,d I want to

  • 0

I have comma separated data in a column:

Column 
------- 
a,b,c,d 

I want to split the comma separated data into multiple columns to get this output:

Column1  Column2 Column3 Column4 
-------  ------- ------- -------
a        b       c       d 

How can this be achieved?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:25:13+00:00Added an answer on May 27, 2026 at 6:25 pm

    If the number of fields in the CSV is constant then you could do something like this:

    select a[1], a[2], a[3], a[4]
    from (
        select regexp_split_to_array('a,b,c,d', ',')
    ) as dt(a)
    

    For example:

    => select a[1], a[2], a[3], a[4] from (select regexp_split_to_array('a,b,c,d', ',')) as dt(a);
     a | a | a | a 
    ---+---+---+---
     a | b | c | d
    (1 row)
    

    If the number of fields in the CSV is not constant then you could get the maximum number of fields with something like this:

    select max(array_length(regexp_split_to_array(csv, ','), 1))
    from your_table
    

    and then build the appropriate a[1], a[2], ..., a[M] column list for your query. So if the above gave you a max of 6, you’d use this:

    select a[1], a[2], a[3], a[4], a[5], a[6]
    from (
        select regexp_split_to_array(csv, ',')
        from your_table
    ) as dt(a)
    

    You could combine those two queries into a function if you wanted.

    For example, give this data (that’s a NULL in the last row):

    => select * from csvs;
         csv     
    -------------
     1,2,3
     1,2,3,4
     1,2,3,4,5,6
    
    (4 rows)
    
    => select max(array_length(regexp_split_to_array(csv, ','), 1)) from csvs;
     max 
    -----
       6
    (1 row)
    
    => select a[1], a[2], a[3], a[4], a[5], a[6] from (select regexp_split_to_array(csv, ',') from csvs) as dt(a);
     a | a | a | a | a | a 
    ---+---+---+---+---+---
     1 | 2 | 3 |   |   | 
     1 | 2 | 3 | 4 |   | 
     1 | 2 | 3 | 4 | 5 | 6
       |   |   |   |   | 
    (4 rows)
    

    Since your delimiter is a simple fixed string, you could also use string_to_array instead of regexp_split_to_array:

    select ...
    from (
        select string_to_array(csv, ',')
        from csvs
    ) as dt(a);
    

    Thanks to Michael for the reminder about this function.

    You really should redesign your database schema to avoid the CSV column if at all possible. You should be using an array column or a separate table instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data stored in a xml column and need a comma-separated list
Hi I have problem with fetching data from one column separated with comma, My
I have the following data in my database (comma separated strings): word, test, hello
I have a method that reads data from a comma separated text file and
I have Text file that contains data separated with a comma , . How
I have a list of input words separated by comma. I want to sort
I have a comma separated value table that I want to read in Python.
Using .NET I have a text file with comma separated data. One of the
I have a little problem with export data to csv (comma-separated values). All data
I have a little problem with export data to csv (comma-separated values). All data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.