PostgreSQL newline character

后端 未结 1 1575
面向向阳花
面向向阳花 2021-02-02 04:54

How to use newline character in PostgreSQL?

This is an incorrect script from my experiment:

select \'test line 1\'||\'\\n\'||\'         


        
相关标签:
1条回答
  • 2021-02-02 05:55

    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:

    select 'test line 1'||E'\n'||'test line 2';
    

    Another option is to use the chr() function:

    select 'test line 1'||chr(10)||'test line 2';
    

    Or simply put the newline in the string constant:

    select 'test line 1
    test line 2';
    

    Whether or not this is actually displayed as two lines in your SQL client, depends on your SQL client.


    update: a good answer from @thedayturns, where you can have a simpler query:

    E'test line 1\ntest line 2'

    0 讨论(0)
提交回复
热议问题