PSQL add line breaks to a string - postgresql

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.

Related

Take. a particular string from a TEXT in postgres

I have one question:
Below is an error message which is stored in a postgres table column, From this string i would like to extract only a part of the string, Is that possible to do in Postgres?
I would like to see odoo.exceptions.ValidationError: ('No MRP template was found for MO/10881!', None)' only this part.
In general all text starting with odoo.exceptions.ValidationError: until the end
How can i do it ? Any idea or suggestions?
'Traceback (most recent call last):
File "/opt/src/addons_OCA/queue/queue_job/controllers/main.py", line 101, in runjob
self._try_perform_job(env, job)
File "/opt/src/addons_OCA/queue/queue_job/controllers/main.py", line 61, in _try_perform_job
job.perform()
File "/opt/src/addons_OCA/queue/queue_job/job.py", line 466, in perform
self.result = self.func(*tuple(self.args), **self.kwargs)
File "/opt/src/addons/costs/models/mrp_production.py", line 163, in trigger_calculate_all_costs
self.calculate_all_costs()
File "/opt/src/addons/sucosts/models/costline_mixin.py", line 284, in calculate_all_costs
rec.generate_service_products()
File "/opt/src/addons/mrp_product_templates/models/mrp_production.py", line 660, in generate_service_products
MO=self.name)))
odoo.exceptions.ValidationError: ('No MRP template was found for MO/10881!', None)'
You can use regexp_replace function to search for particular text then select the text following.
The regexp_replace function provides substitution of new text for
substrings that match POSIX regular expression patterns. It has the
syntax regexp_replace(source, pattern, replacement [, flags ]). The
source string is returned unchanged if there is no match to the
pattern. ...
In this case it seems you want the text after ValidationError:. Something like: (see demo)
select regexp_replace (message, '.*ValidationError:(.*)','\1')
from test;

How to find text pattern in string for postgresql SQL

For this sample string: "... Key Match extra text..."
How do I get the value "Match", which is the string between blank spaces after "Key"?
is there a better way than:
Find position of "Key "->pos1, find position of first blank space after p1 -> p2, substring(string, p1,p2)?
This is not working as I expected
Select substring('Key Match extra text', 'Key (.+) ');
---
Match extra
You can make the regex be "non-greedy", so that .+ matches as few as possible:
Select substring('Key Match extra text', 'Key (.+?) ');
Or you can change . to something that won't match spaces:
Select substring('Key Match extra text', 'Key (\S+) ');

DB2 remove empty lines

I have strings like this
#
word_1
word_2
#
word_3
#
#
where # represents empty lines.
I'd like to remove those empty lines, for getting
word_1
word_2
word_3
I've tried replacing CHR(10) and CHR(13) with '' but then I get
word_1word_2word_3
I've seen I can remove the first empty line using LTRIM, but how to get rid of all of them?
You must remove all new-line characters followed by new-line character, and a single new-line character at the start and the end of a string. All these replacements can be done with a single expression.
Starting from v11.1
select regexp_replace (s, '\r\n(?=\r\n)|^\r\n|\r\n$', '')
from (values x'0d0a' || 'abc' || x'0d0a0d0a'|| 'def' || x'0d0a') t (s)
Note, that you may have a new-line character encoded as x'0a' instead of x'0d0a'. Remove all the \r characters in this case from the expression above.
dbfiddle link.
Starting from v9.7
select xmlcast (xmlquery ('replace (replace ($d, "^\r\n|\r\n$", ""), "(\r\n){2,}", "$1")' passing s as "d") as varchar (100))
from (values x'0d0a' || 'abc' || x'0d0a0d0a'|| 'def' || x'0d0a') t (s)
dbfiddle link.

Redshift: how to remove all newline characters in a field

I am wondering how I can remove all newline characters in Redshift from a field. I tried something like this:
replace(replace(body, '\n', ' '), '\r', ' ')
and
regexp_replace(body, '[\n\r]+', ' ')
But it didn't work. Please share if you know how to do this.
Use chr(10) instead of \n
example:
select replace(CONCAT('Text 1' , chr(10), 'Text 2'), chr(10), '-') as txt
This should help
regexp_replace(column, '\r|\n', '')
To remove line breaks:
SELECT REPLACE('This line has
a line break', CHR(10), '');
This gives output: This line hasa line break. You can see more ASCII or CHR() codes here: https://www.petefreitag.com/cheatsheets/ascii-codes/
To remove special characters like \r, \n, \t
Assuming col1 has text like This line has\r\n special characters.
Using replace()
SELECT REPLACE(REPLACE(col1, '\\r', ''), '\\n', '');
We need to escape \ because backslash is special character in SQL (used to escape double quotes, etc...)
Using regexp_replace()
SELECT REGEXP_REPLACE(col1, '(\\\\r|\\\\n)', '');
We need to escape \ because it is a special character in SQL and we need to escape the resulting backslashes again because backslash is a special character in regex as well.
Both replace() and regexp_replace() gives output: This line has special characters.

Edit text value in Postgresql

I have a text column which holds a json with very long values - actually HTML code.
{"first_key" : "a very long html", "second_key" : "another very long html"}
How can I easily delete the second_key and its vale?
You could use regex_replace, and hope that the second html doesn't contain a } character:
update MY_TABLE set text_column = regex_replace(text_column, ', "second_key": ".*\}"', '')
where my_column_id = '<whatever>';
You will have to tweak your regex to make this work. You can test it by doing a select with the same expression to see what you get.