pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
how to concat/concat_ws all fields without braces
6+ messages / 3 participants
[nested] [flat]

* how to concat/concat_ws all fields without braces
@ 2019-06-15 06:19  Jean Louis <bugs@gnu.support>
  0 siblings, 2 replies; 6+ messages in thread

From: Jean Louis @ 2019-06-15 06:19 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hello,

I have tried doing something like:

SELECT concat_ws(' ', table.*) FROM table;

and if I do that way, it is essentially same as 

SELECT concat(table.*) FROM table;

and I get the items in braces like (1,something).

Why do I get it in braces?

Is there a way without specifying specific fields
to get all items concatenated without braces?

I would prefer conat_ws option.

Jean





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: how to concat/concat_ws all fields without braces
@ 2019-06-15 09:36  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Jean Louis <bugs@gnu.support>
  1 sibling, 1 reply; 6+ messages in thread

From: Pavel Stehule @ 2019-06-15 09:36 UTC (permalink / raw)
  To: Jean Louis <bugs@gnu.support>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

Hi

so 15. 6. 2019 v 8:20 odesílatel Jean Louis <bugs@gnu.support> napsal:

> Hello,
>
> I have tried doing something like:
>
> SELECT concat_ws(' ', table.*) FROM table;
>
> and if I do that way, it is essentially same as
>
> SELECT concat(table.*) FROM table;
>
> and I get the items in braces like (1,something).
>
> Why do I get it in braces?
>
> Is there a way without specifying specific fields
> to get all items concatenated without braces?
>
> I would prefer conat_ws option.
>

It cannot to work. Postgres try to convert composite type based on all
fields to one text value, and this value is passed as one argument. It can
work because concat, concat_ws has 'variadic "any" parameter. But it does
cannot to work like you expect.

you can write own function that will do what you want

create or replace function rec_concat_fields(record, text)
returns text as $$
  begin
    return string_agg(value, '|') from json_each_text(row_to_json($1));
  end
$$ language plpgsql;

postgres=# select rec_concat_fields(foo.*, '*') from foo;
┌───────────────────┐
│ rec_concat_fields │
╞═══════════════════╡
│ ahoj|svete        │
└───────────────────┘
(1 row)

Regards

Pavel




> Jean
>
>
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: how to concat/concat_ws all fields without braces
@ 2019-06-15 09:36  Thomas Kellerer <spam_eater@gmx.net>
  parent: Jean Louis <bugs@gnu.support>
  1 sibling, 1 reply; 6+ messages in thread

From: Thomas Kellerer @ 2019-06-15 09:36 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Jean Louis schrieb am 15.06.2019 um 13:19:
> I have tried doing something like:
> 
> SELECT concat_ws(' ', table.*) FROM table;
> 
> and if I do that way, it is essentially same as 
> 
> SELECT concat(table.*) FROM table;
> 
> and I get the items in braces like (1,something).
> 
> Why do I get it in braces?
> 
> Is there a way without specifying specific fields
> to get all items concatenated without braces?
> 
> I would prefer conat_ws option.

you can use the json functions for that:

  select (select string_agg(x.v, ',') from jsonb_each_text(to_jsonb(t)) as x(k,v)) as all_columns
  from the_table t;








^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: how to concat/concat_ws all fields without braces
@ 2019-06-15 14:20  Jean Louis <bugs@gnu.support>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Jean Louis @ 2019-06-15 14:20 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

Dear Pavel,

Ahoj.

* Pavel Stehule <pavel.stehule@gmail.com> [2019-06-15 11:37]:
> you can write own function that will do what you want
> 
> create or replace function rec_concat_fields(record, text)
> returns text as $$
>   begin
>     return string_agg(value, '|') from json_each_text(row_to_json($1));
>   end
> $$ language plpgsql;
> 
> postgres=# select rec_concat_fields(foo.*, '*') from foo;
> ┌───────────────────┐
> │ rec_concat_fields │
> ╞═══════════════════╡
> │ ahoj|svete        │
> └───────────────────┘
> (1 row)
> 

I have tried that one, not working quite;

create or replace function concat_fields(record, text)
returns text as $$
  begin
    return string_agg(value, ' ') from json_each_text(row_to_json($1));
  end
$$ language plpgsql;
rcdbusiness$# ERROR:  PL/pgSQL functions cannot accept type record

Jean





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: how to concat/concat_ws all fields without braces
@ 2019-06-15 14:22  Jean Louis <bugs@gnu.support>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 6+ messages in thread

From: Jean Louis @ 2019-06-15 14:22 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-general@lists.postgresql.org

Dear Thomas,

* Thomas Kellerer <spam_eater@gmx.net> [2019-06-15 11:37]:
> Jean Louis schrieb am 15.06.2019 um 13:19:
> > I have tried doing something like:
> > 
> > SELECT concat_ws(' ', table.*) FROM table;
> > 
> > and if I do that way, it is essentially same as 
> > 
> > SELECT concat(table.*) FROM table;
> > 
> > and I get the items in braces like (1,something).
> > 
> > Why do I get it in braces?
> > 
> > Is there a way without specifying specific fields
> > to get all items concatenated without braces?
> > 
> > I would prefer conat_ws option.
> 
> you can use the json functions for that:
> 
>   select (select string_agg(x.v, ',') from jsonb_each_text(to_jsonb(t)) as x(k,v)) as all_columns
>   from the_table t;

Danke, that works well. I did not know it works
through Jansson exports. I hope it is fast enough.

Jean






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: how to concat/concat_ws all fields without braces
@ 2019-06-15 14:37  Pavel Stehule <pavel.stehule@gmail.com>
  parent: Jean Louis <bugs@gnu.support>
  0 siblings, 0 replies; 6+ messages in thread

From: Pavel Stehule @ 2019-06-15 14:37 UTC (permalink / raw)
  To: Jean Louis <bugs@gnu.support>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>

so 15. 6. 2019 v 16:20 odesílatel Jean Louis <bugs@gnu.support> napsal:

> Dear Pavel,
>
> Ahoj.
>
> * Pavel Stehule <pavel.stehule@gmail.com> [2019-06-15 11:37]:
> > you can write own function that will do what you want
> >
> > create or replace function rec_concat_fields(record, text)
> > returns text as $$
> >   begin
> >     return string_agg(value, '|') from json_each_text(row_to_json($1));
> >   end
> > $$ language plpgsql;
> >
> > postgres=# select rec_concat_fields(foo.*, '*') from foo;
> > ┌───────────────────┐
> > │ rec_concat_fields │
> > ╞═══════════════════╡
> > │ ahoj|svete        │
> > └───────────────────┘
> > (1 row)
> >
>
> I have tried that one, not working quite;
>
> create or replace function concat_fields(record, text)
> returns text as $$
>   begin
>     return string_agg(value, ' ') from json_each_text(row_to_json($1));
>   end
> $$ language plpgsql;
> rcdbusiness$# ERROR:  PL/pgSQL functions cannot accept type record
>

depends on Postgres version. I have 12, but probably it should to work with
Postgres 10, 11

Pavel


> Jean
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2019-06-15 14:37 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-06-15 06:19 how to concat/concat_ws all fields without braces Jean Louis <bugs@gnu.support>
2019-06-15 09:36 ` Pavel Stehule <pavel.stehule@gmail.com>
2019-06-15 14:20   ` Jean Louis <bugs@gnu.support>
2019-06-15 14:37     ` Pavel Stehule <pavel.stehule@gmail.com>
2019-06-15 09:36 ` Thomas Kellerer <spam_eater@gmx.net>
2019-06-15 14:22   ` Jean Louis <bugs@gnu.support>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox