Postgresql add double quote to string while COPY-TO - postgresql

I'm writting a file from a DB-Procedure.
I need to export a string value and I have to add double quotes in front and back of the string.
exp: "TEST".
I use the following code but the result is always like """TEST""" and I don't know why.
EXECUTE 'COPY (
SELECT concat(''"'', x.text1, ''"'')
FROM my_table x
) to ''' || dump_path || ''' WITH (FORMAT csv, DELIMITER '','', ENCODING ''WIN1252'')';

Related

PostgreSQL: using strings in SQL subquery text in a function

I'm using the pgr_drivingDistance function that requires a SQL subquery as text like so:
pgr_drivingDistance(
'SELECT id, source, target, cost, reverse_cost FROM edges', start_vid, dist, true
)
I would like to subset the edges in the edges table with a where clause like so:
pgr_drivingDistance(
'SELECT id, source, target, cost, reverse_cost FROM edges WHERE col = 'some_string'', start_vid, dist, true
)
The problem is I cannot use single quotes in this case for the string. I tried to escape the quotes of the string with ''', backslash, $$ notations without success.
Is there a way to do this?
There's various ways to escape single quotes (apostrophes) in Postgres string literals:
"To include a single-quote character within a string constant, write two adjacent single quotes"
pgr_drivingDistance('SELECT … WHERE col = ''some_string'' ', start_vid, dist, true)
"A single quote can be included in an escape string by writing \', in addition to the normal way of ''."
pgr_drivingDistance(e'SELECT … WHERE col = \'some_string\' ', start_vid, dist, true)
"Inside the dollar-quoted string, single quotes can be used without needing to be escaped"
pgr_drivingDistance($$SELECT … WHERE col = 'some_string'$$, start_vid, dist, true)

How to add a single quote before each comma

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
A literal quote is coded as a doubled quote:
select '(''' || replace(mycolumn, ',', ''',''') || ''')'
from mytable
See live demo.

PSQL - encrypt_iv return multiple line encoded text

I am facing a issue to load a csv resulting froma query that encryts a text.
This is the query I run:
select
id,
encode(encrypt_iv(raw_address::bytea, '<aes_key>', '<iv>', 'aes-cbc/pad:pkcs'), 'base64') raw_address
from some_table;
But I got a multiline text as result for raw_address column. So I tried:
select
id,
encode(encrypt_iv(replace(raw_address, chr(92), chr(47))::bytea, '<aes_key>', '<iv>', 'aes-cbc/pad:pkcs'), 'base64') raw_address
from some_table;
This because I just wanted to make this \ into this / (to avoid \n)
This is the result example:
But got the same result. Then I found this answer and realize that + char was present, so I tried:
select
id,
replace(encode(encrypt_iv(replace(raw_address, chr(92), chr(47))::bytea, '<aes_key>', '<iv>', 'aes-cbc/pad:pkcs'), 'base64'), chr(10), '')
from, some_table;
Then I got one line:
But I don't know if I am modifing the original value, I can not decrypt the value. I tried:
select encode(decrypt_iv('55WHZ7tyGAlQxTIM0fPfY5tOKpbYzwdXCsemIgYV5TRG+h45IW1nU/zCqZbkIeiXQ3OXZSlHo0RPgq5wcgJ0xQ==', '<aes_key>', '<iv>', 'aes-cbc/pad:pkcs'), 'base64') ;
But I got:
ERROR: decrypt_iv error: Data not a multiple of block size
Any suggestion will be appreciated.
Thanks in advance!

postgresql replace symbol for store function variable?

I find '#' can replace some string in posgresql like:
SELECT
'Record #' || i
FROM
generate_series(1, 1000) i;
The symbol '#' can replace the by i, in this SQL commands.
I want to do similar like this for function like
SELECT lo_unlink('#') || i
From
SELECT picture i FROM image where belong_to = 254;
Of course '#' there is grammar error around '#', I just want to know any placehold grammar or symbol can work in function parameter like it replace string mark '#'
The symbol # is not replaced, you are concatenating a number to a string containing this symbol.
This concatenation can occur anywhere, including when building a function parameter:
SELECT lo_unlink('#' || i)
From
SELECT picture i FROM image where belong_to = 254;
which, if i is number 1, will create (and call) lo_unlink('#1')
so you may want to use i directly:
SELECT lo_unlink(i)
From
SELECT picture i FROM image where belong_to = 254;
which, if i is number 1, will create (and call) lo_unlink(1)

LIKE with % on column names

Here is my query that results in a syntax error:
SELECT *
FROM account_invoice,sale_order
WHERE sale_order.name LIKE %account_invoice.origin%
The account_invoice.origin field contains the text of sale_order.name, plus other text as well, so I need to match sale_order.name string anywhere in the account_invoice.origin string.
I'm using PostgreSQL 8.4.
Try this
SELECT *
FROM account_invoice,sale_order
WHERE sale_order.name LIKE '%' || account_invoice.origin || '%'
% needs single quote because the pattern is a string.
|| is the operator for concatenation.