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
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.
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.
The PostgreSQL concatenate operator ( || ) is used to concatenate two or more strings and non strings.
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.
select rtrim(ltrim(replace(tablename::text, ',', ''), '('), ')') from tablename;
I would propose two options. They both work using table_row to text
technique.
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)
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With