encode a text which contains hex string into utf-8 - postgresql

I'm struggling to encode this hex string: =D8=A8=D8=A7 <br /> =D8=B3=D9=84=D8=A7=D9=85 hello =D9=88 =D8=A7=D8=AD=D8=AA=D8=B1=D8=A7= =D9=85 into proper format which must be utf-8 and must be displayed as :
با سلام hello و احترا= م
What I've tried so far is that at first step I've tried to decode hex string with decode function but since there are invalid digit in my hex string (=) it throws error:
select decode(content, 'hex') from attachments
ERROR: invalid hexadecimal digit: "="
I also tried to directly convert it to utf-8 but nothing has changed in output:
select convert_from(content::bytea, 'utf-8') from attachments
=D8=A8=D8=A7 <br /> =D8=B3=D9=84=D8=A7=D9=85 hello =D9=88 =D8=A7=D8=AD=D8=AA=D8=B1=D8=A7= =D9=85

Try something like this:
select convert(decode(replace(replace(content,'=',''),' ','20'), 'hex'),'UTF8') from attachments
or
select convert_from(decode(replace(replace(content,'=',''),' ','20'), 'hex')::bytea, 'utf-8') from attachments

Related

How to cast a string to timestamp9

I have strings like this: '2022-02-28T04:45:37.123456789Z'
Does anyone know why this fails:
select '2022-02-28T04:45:37.123456789Z'::timestamp9
SQL Error [22008]: ERROR: invalid input format for timestamp9, required format y-m-d h:m:s.ns +tz "2022-02-28T04:45:37.123456789Z"
Position: 8
whereas this is fine:
select '2022-02-28T04:45:37Z'::timestamp9
What I found, using timestamp9 extension.
create extension timestamp9 ;
select '2022-02-28T04:45:37.123456789Z'::timestamp9;
ERROR: invalid input format for timestamp9, required format y-m-d h:m:s.ns +tz "2022-02-28T04:45:37.123456789Z"
LINE 1: select '2022-02-28T04:45:37.123456789Z'::timestamp9;
--What I thought would work, but didn't
select '2022-02-28T04:45:37.123456789 +0000'::timestamp9;
ERROR: invalid input format for timestamp9, required format y-m-d h:m:s.ns +tz "2022-02-28T04:45:37.123456789 +0000"
LINE 1: select '2022-02-28T04:45:37.123456789 +0000'::timestamp9;
--What does work
select '2022-02-28 04:45:37.123456789 +0000'::timestamp9;
timestamp9
-------------------------------------
2022-02-27 20:45:37.123456789 -0800
--A quick and dirty solution
select replace(replace('2022-02-28T04:45:37.123456789Z', 'T', ' '), 'Z', ' +0000')::timestamp9;
replace
-------------------------------------
2022-02-27 20:45:37.123456789 -0800

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!

PSQL add line breaks to a string

I have a function in PSQL that sends an email to a particular user. The function takes an email text argument and adds this to some constant text before and after in the email body:
In this function, I have a json_build_object call which deals with the recipient, body and title of the email:
jsonb_build_object(
'recipient',recipient,
'title',title,
'message_text', 'some text before.
some text on new line ' || body ||
' some text after.
some text on new line.'
)
My problem is, in the text before and after body, I can't format this on to separate lines. I have tried the following but it did not work:
jsonb_build_object(
'recipient',recipient,
'title',title,
'message_text', E'some text before.\n
some text on new line \n' || body ||
E'\nsome text after.\n
some text on new line.'
)
Can anyone advise what I am doing wrong?
You do it correctly, and the result has line breaks in the form \n. That's how line breaks in text look in JSON.
RFC 8259 describes this:
A string begins and ends with
quotation marks. All Unicode characters may be placed within the
quotation marks, except for the characters that MUST be escaped:
quotation mark, reverse solidus, and the control characters (U+0000
through U+001F).
Line feed is character U+000A, so it must be escaped.
If you want unescaped line breaks, use something else than JSON.
Laurenz Albe pointed out the issue from the JSON end. My answer is about the other end.
What are you doing with the JSON to get it into an email?
It might be possible to do what you want from there. An example in Python:
select * from json_build_object(
'recipient', 'aklaver',
'title', 'head bottlewasher',
'message_text', 'some text before.\nsome text on new line ' || ' The body'||' some text after.\nsome text on new line.'
);
json_build_object
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"recipient" : "aklaver", "title" : "head bottlewasher", "message_text" : "some text before.\\nsome text on new line The body some text after.\\nsome text on new line."}
email_str = "some text before.\\nsome text on new line The body some text after.\\nsome text on new line."
print(email_str.replace("\\n", "\n"))
some text before.
some text on new line The body some text after.
some text on new line.

Error in Pentaho Data Integrator - invalid byte sequence for encoding "UTF8": 0x00

Error getting while insert bulk rows with Pentaho Data Interrogator. I am using PostgreSQL
ERROR: invalid byte sequence for encoding "UTF8": 0x00
"UTF8": 0x00 = "null character". You can use "Modified Javascript" step, and then apply a mask pattern as follows:
function removeNull(e) {
if(e != null)
return e.replace(/\0/g, '');
else
return '';
}
var replacedString = removeNull(fieldToRemoveNullChars);
Select the new field for the Modified Javascript output, and voilla!. Use to have this problem with AS400 incoming data.
PostgreSQL is very strict content of text fields, and doesn't allow 0x00 in utf8 encoded fields. You should to fix your input data.
Some possible solution https://superuser.com/questions/287997/how-to-use-sed-to-remove-null-bytes
Finally I got the solution:
In Table Input, check the "Enable lazy conversion" option
Enter the "Select Values" step Select all fields and on the forced "Metadata" tab by entering the "UTF-8" encoding for all fields.

Output file using ISQL

Already I have a ISQL script working with Interbase that works with output into csv and I want to do the same thing to another type, but is not for example the first code works and it works . But the second one using Join statement is only working in the sql window, but could not output it.
Working :
OUTPUT TESTING1.csv FORMAT ASCII DELIMITED BY ';' QUOTE '';
Select * from aircraft;
OUTPUT;
Not working :
OUTPUT TESTING1.csv FORMAT ASCII DELIMITED BY ';' QUOTE '';
SELECT * FROM aircraft
Join Operation ON aircraft.AC_ID = Operation.ac_ID
OUTPUT;
I got it working now realise that I need to put the semi colon after the join statement:
OUTPUT TESTING1.csv FORMAT ASCII DELIMITED BY ';' QUOTE '';
SELECT * FROM aircraft
Join Operation ON aircraft.AC_ID = Operation.ac_ID;
OUTPUT;