Search code examples
sqlpostgresqlphp-pgsql

Postreg SQL get All Value with quotes from the comma separated


I have Values which are stored like 1,2,3,4,5 in the database.

I want it back like '1','2','3','4','5'.

I am trying with string_agg(format('''%s''', ticker_information.user_groups), ',') but giving me result '1,2,3,4,5'

Any solution ? Or let me know If I am doing wrong.

Thanks


Solution

  • Try this if you just want a string back with the quotes

    WITH sample AS (
      SELECT '1,2,3,4,5'::text as test
    )
    
    SELECT
      '''' || array_to_string(
        string_to_array(test, ','),
        ''','''
      ) || ''''
    FROM sample