Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres concat all

I need connect all columns (unknown columns) from every rows to one string

I want do sth like this:

select concat(*) from table;

The problem is that the function concat doesn't take parameter "*"

Anyone can help me?

Example:

Table

GID | Name
----------
1   | nameA
2   | nameB
3   | nameC

I need on output:

1nameA
2nameB
3nameC
like image 819
k2n Avatar asked Jan 21 '13 01:01

k2n


People also ask

How do I concatenate values in PostgreSQL?

In PostgreSQL, the CONCAT function is used to concatenate two or more strings into one. Let's analyze the above syntax: The CONCAT function accepts a list of string convertible arguments. A string in this context means any of the following data types: char, varchar, or text.

How do I concatenate columns in PostgreSQL?

PostgreSQL allows you to directly concatenate strings, columns and int values using || operator. Here is the SQL query to concatenate columns first_name and last_name using || operator. You can even concatenate string with int using || operator.

What is || in PostgreSQL?

The PostgreSQL concatenate operator ( || ) is used to concatenate two or more strings and non strings.

How do I join strings in PostgreSQL?

The PostgreSQL concat() function is used to concatenate two or more strings except NULL specified in the arguments. Example: PostgreSQL CONCAT() function: In the example below the strings specified in the argument have been concatenated and returns a string.


2 Answers

select rtrim(ltrim(replace(tablename::text, ',', ''), '('), ')') from tablename;
like image 142
klin Avatar answered Sep 21 '22 12:09

klin


I would propose two options. They both work using table_row to text technique.

  1. Quick and dirty:

    select r::text from some_table AS r
    

    Sample output:

    (289,310,,2010-09-10,6,0,1,6,0,30514,6,882,8,4,1,7,2,2,3,1,2,2,2,1,2,2,2,,1,51,0,0,0,0,0,1386,1,1,,6,,0,,,010100002082080000B3EA73156DA25C411E85EB61CB155641)
    
  2. Quick with a possibility to manipulate the data:

    select translate(string_to_array(r::text, ',')::text, '()', '')::text[] from some_table AS r
    

    which returns an actual array of text (text[]) on which any array function can be applied :)
    Sample output (note starting and ending parenthesis type):

    {289,310,"",2010-09-10,6,0,1,6,0,30514,6,882,8,4,1,7,2,2,3,1,2,2,2,1,2,2,2,"",1,51,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1386,1,1,"",6,"",0,"","",010100002082080000B3EA73156DA25C411E85EB61CB155641}
    

Using the second approach and array_to_string(array, delimiter) function you can concatenate all column textual representations into one string. Just pick a delimiter (e.g. ',', '|' or even ''). For example using '|' you end up with a query:

select array_to_string(translate(string_to_array(r::text, ',')::text, '()', '')::text[], '|') from some_table AS r

with sample output:

289|310||2010-09-10|6|0|1|6|0|30514|6|882|8|4|1|7|2|2|3|1|2|2|2|1|2|2|2||1|51|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|2|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|1|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|1386|1|1||6||0|||010100002082080000B3EA73156DA25C411E85EB61CB155641

Hope that helps :)

like image 30
andr Avatar answered Sep 22 '22 12:09

andr