Home > OS >  How to add a single quote before each comma
How to add a single quote before each comma

Time:03-18

a have a column as below

mystring
AC1853551,AC1854125,AC1855220,AC188115,AC1884120,AC1884390,AC1885102

I need to transformm it to get this output

mystring
('AC1853551','AC1854125','AC1855220','AC188115','AC1884120','AC1884390','AC1885102')

Here is my query that i tried

select  CONCAT('( , CONCAT (mystring, ')')) from mytablename 

I'm getting an error when it comes to insert a single quote '

Then i thought about replacing the comma with a ','

How to get desired output

i'm using postgres 10

CodePudding user response:

A literal quote is coded as a doubled quote:

select '(''' || replace(mycolumn, ',', ''',''') || ''')'
from mytable

See live demo.

  • Related