问题描述
如何在 PostgreSQL
中使用换行符
?
这是我实验中不正确的脚本:
This is an incorrect script from my experiment:
select 'test line 1'||'\n'||'test line 2';
我希望 sql编辑器
显示此结果从上面的脚本中获取
I want the sql editor
display this result from my script above:
test line 1
test line 2
但是不幸的是,当我在sql编辑器中运行脚本时,只是从脚本中得到以下结果:
But unfortunately I just get this result from my script when I run it in sql editor:
test line 1 test line 2
推荐答案
反斜杠在SQL中没有特殊含义,因此'\n'
是反斜杠,后跟字符 n
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';
另一种选择是使用 chr()
函数:
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';
是否实际显示 作为SQL客户端中的两行,取决于您的SQL客户端。
Whether or not this is actually displayed as two lines in your SQL client, depends on your SQL client.
这篇关于PostgreSQL换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!