1

I can't make PostgreSql split a string into multiple strings based on newline characters. I have tried splitting by: \n, \r, \r\n. But it's not working!

Not working code:

SELECT string_to_array('Test1
Test2', '\r\n')

Working code (newline character copied directly from text, but it is displayed as invisible \r\n or CRLF):

SELECT string_to_array('Test1
    Test2', '
')

Initial data - a table, the content of which is imported from a .csv file.

What am I doing wrong?

1 Answer 1

3

After a quick search for "Postgresql Newline character", I've found PostgreSQL newline character

Copied Answer:

the backslash has no special meaning in SQL, so '\n' is a backslash followed by the character n
To use "escape sequences" in a string literal you need to use an "extended" constant:

In your case

SELECT string_to_array('Test1
Test2', E'\n')

Result

string_to_array
{Test1,Test2}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.