0

I tried the method :

STRING_AGG(cast(client.nom as varchar(1000)), '
                ')

In order to produce a line break when exporting my query results to CSV.

This doesn't give me only one line break, but at least 5 for each record.

How to make a single line break between each record in a "string_agg" on PostgreSQL?

Thank you.

  • Try `string_agg(urgence.nom, chr(10))` - the cast is pretty much useless. –  Oct 27 '21 at 07:43
  • Perhaps there are a lot of empty strings in your table. – Laurenz Albe Oct 27 '21 at 07:46
  • Thank you a_horse_with_no_name, indeed it works! I followed your advice and deleted the cast, then indicated chr(10). –  Oct 27 '21 at 07:52
  • A CSV file does contain one linebreak between each row, and no other linebreaks. There is no way to change that, and nothing that `string_agg` needs to do about this. Can you show us how you are doing the CSV export, please? – Bergi Oct 27 '21 at 09:16
  • @a_horse_with_no_name `chr(10)` seems like a very obscure way to write `e'\n'`. Why would you prefer that? – Bergi Oct 27 '21 at 09:18
  • @Bergi: because I "grew up" with that (e.g. in Oracle) and not the the `e'..'` notation ;) –  Oct 27 '21 at 09:23
  • @a_horse_with_no_name Oh right I didn't realise that escape sequences are non-standard sql – Bergi Oct 27 '21 at 09:26
  • @Bergi My problem was not with the csv but with the postgres "string_agg" formatting. This was solved with the method mentioned by a_horse_with_no_name. –  Oct 27 '21 at 10:37

0 Answers0