Alex
Alex

Reputation: 31

PostgreSql not splitting string by new line

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?

Upvotes: 1

Views: 1310

Answers (1)

A.Copter
A.Copter

Reputation: 360

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}

Upvotes: 2

Related Questions