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
PostgreSQL newline character
提问by Alimin
How to use newline character
in PostgreSQL
?
如何使用newline character
的PostgreSQL
?
This is an incorrect script from my experiment:
这是我实验中的错误脚本:
select 'test line 1'||'\n'||'test line 2';
I want the sql editor
display 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 客户端。