SQL PostgreSQL 换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36028908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:29:44  来源:igfitidea点击:

PostgreSQL newline character

sqlpostgresql

提问by Alimin

How to use newline characterin PostgreSQL?

如何使用newline characterPostgreSQL

This is an incorrect script from my experiment:

这是我实验中的错误脚本:

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

I want the sql editordisplay this result from my script above:

我希望sql editor从上面的脚本中显示此结果:

test line 1
test line 2

But unfortunately I just get this result from my script when I run it in sql editor:

但不幸的是,当我在 sql 编辑器中运行它时,我只是从我的脚本中得到这个结果:

test line 1 test line 2

回答by a_horse_with_no_name

The backslash has no special meaning in SQL, so '\n'is a backslash followed by the character n

反斜杠在 SQL 中没有特殊含义,'\n'反斜杠后跟字符也是如此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:

另一种选择是使用该chr()功能:

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 displayedas two lines in your SQL client, depends on your SQL client.

这是否在您的 SQL 客户端中实际显示为两行,取决于您的 SQL 客户端。