How to add a single quote before each comma - postgresql

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.

Related

Why Does string_agg Not Make Changes In This Context

I was starting down the road of testing within group(), and I came across this unexpected behavior of string_agg (or maybe substring). In the following code, mType is a field containing things like 'HMA - etc.etc.etc.' or 'ACP - etc.etc.etc.' In the following context, the 2nd selected field fails to change the delimiter from a ',' to a '-'
select string_agg( substring( mType,1,3 ), ',' ) mType
,string_agg( substring( mType,1,3), '-' ) mType2
from ourDB..mTypeTable
where ref = '3944900'
Returns:
If I change the substring from 1,3 to 1,4, then things work.
select string_agg( substring( mType,1,3 ), ',' ) mType
,string_agg( substring( mType,1,4), '-' ) mType2
from ourDB..mTypeTable
where ref = '3944900'
Returns:
Why does TSQL fail to change the delimiter in the first context? Is this some kind of optimization moment or something where TSQL is reusing the last substring because it's pulling from the same field in the same table in the same select?

Square brackets in PgAdmin 4 for null values

In pgAdmin 4, the column value is seen as a square bracket [...] instead of an empty value.
The column data type is character(4) and name is carr_desig_icao_cd. Database is postgreSql.
How to avoid the square brackets? I tried pgAdmin 4 preferences but no luck.
Thanks for your help.
Output from psql is as below:
Could it be something to do with converting to_char?
Here are results of my testing.
This query produced no brackets and dots:
select at1.score
This query produced brackets and dots
select to_char(at1.score, '999')
I noticed when this was downloaded and opened in Excel, there were no brackets but three spaces at the start of the column.
This query removed the brackets and dots in PgAdmin and removed the spaces after downloading to Excel:
select replace(to_char(at1.score, '999'), ' ', '')
Just noting also that this was a sub query as part of a bigger query that looked a bit like this:
select
cm.course_id
, us.user_id
, gm.title
, (select replace(to_char(at1.score * 20, '999'), ' ', '') || '% ' || to_char(at1.attempt_date, 'yyyy-mm-dd') from attempt at1 where at1.pk1 = gg.highest_attempt_pk1)
from
etc (joins of course_main cm, users us, gradebook_main gm, gradebook_grade gg, attempt at)
This screenshot shows before and after

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 add double quote to string while COPY-TO

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'')';

PostgreSQL return last n words

How to return last n words using Postgres.
I have tried using LEFT method.
SELECT DISTINCT LEFT(name, -4) FROM my_table;
but it return last 4 characters ,i want to return last 3 words.
demo:db<>fiddle
You can do this using a the SUBSTRING() function and regular expressions:
SELECT
SUBSTRING(name FROM '((\S+\s+){0,3}\S+$)')
FROM my_table
This has been explained here: How can I match the last two words in a sentence in PostgreSQL?
\S+ is a string of non-whitespace characters
\s+ is a string of whitespace characters (e.g. one space)
(\S+\s+){0,3} Zero to three words separated by a space
\S+$ one word at the end of the text.
-> creates 4 words (or less if there are no more).
One way is to use regexp_split_to_array() to split the string into the words it contains and then put a string back together using the last 3 words in that array.
SELECT coalesce(w.words[array_length(w.words, 1) - 2] || ' ', '')
|| coalesce(w.words[array_length(w.words, 1) - 1] || ' ', '')
|| coalesce(w.words[array_length(w.words, 1)], '')
FROM mytable t
CROSS JOIN LATERAL (SELECT regexp_split_to_array(t."name", ' ') words) w;
db<>fiddle
RIGHT() should do
SELECT RIGHT('MYCOLUMN', 4); -- returns LUMN
UPD
You can convert to array and then back to string
SELECT array_to_string(sentence[(array_length(sentence,1)-3):(array_length(sentence,1))],' ','*')
FROM
(
SELECT regexp_split_to_array('this is the one of the way to get the last four words of the string', E'\\s+') AS sentence
) foo;
DEMO HERE