Home > Net >  Split data from a column and ignore the same data and convert that to a row in postgresql
Split data from a column and ignore the same data and convert that to a row in postgresql

Time:10-22

I'm trying to split data from a column but the data sometimes duplicate.

The data will look like this:

TableA
Id  ColumnData
1   Data1,Data2,Data1,Data2,Data3

I want to remove the duplicate in ColumnData and convert that data as a row

the final result will be like this:

ID  ColumnData
1   Data1
1   Data2
1   Data3

Is this possible to do this?

thanks

CodePudding user response:

Convert delimited column data into row in multiple ways.

-- PotgreSQL
SELECT DISTINCT
    Id, 
    regexp_split_to_table(ColumnData, E',') AS ColumnData
FROM TableA

OR

SELECT DISTINCT id, unnest(string_to_array(ColumnData, ',')) ColumnData
FROM TableA

Please check from url https://dbfiddle.uk/?rdbms=postgres_13&fiddle=80674eeef04888537271340c42d68c85

  • Related