PostgreSQL 换行符
PostgreSQL newline character
如何在PostgreSQL
中使用newline character
?
这是我实验中的错误脚本:
select 'test line 1'||'\n'||'test line 2';
我希望 sql editor
显示上面脚本的结果:
test line 1
test line 2
但不幸的是,当我在 sql 编辑器中 运行 时,我只是从我的脚本中得到了这个结果:
test line 1 test line 2
反斜杠在SQL中没有特殊意义,所以'\n'
是一个反斜杠后面跟字符n
要在字符串文字中使用“转义序列”,您需要使用 "extended" constant:
select 'test line 1'||E'\n'||'test line 2';
另一种选择是使用chr()
函数:
select 'test line 1'||chr(10)||'test line 2';
或者简单地将换行符放在字符串常量中:
select 'test line 1
test line 2';
这是否实际上 在您的 SQL 客户端中显示为 两行,取决于您的 SQL 客户端。
更新:@thedayturns 的一个很好的答案,您可以在其中进行更简单的查询:
select E'test line 1\ntest line 2';
如何在PostgreSQL
中使用newline character
?
这是我实验中的错误脚本:
select 'test line 1'||'\n'||'test line 2';
我希望 sql editor
显示上面脚本的结果:
test line 1
test line 2
但不幸的是,当我在 sql 编辑器中 运行 时,我只是从我的脚本中得到了这个结果:
test line 1 test line 2
反斜杠在SQL中没有特殊意义,所以'\n'
是一个反斜杠后面跟字符n
要在字符串文字中使用“转义序列”,您需要使用 "extended" constant:
select 'test line 1'||E'\n'||'test line 2';
另一种选择是使用chr()
函数:
select 'test line 1'||chr(10)||'test line 2';
或者简单地将换行符放在字符串常量中:
select 'test line 1
test line 2';
这是否实际上 在您的 SQL 客户端中显示为 两行,取决于您的 SQL 客户端。
更新:@thedayturns 的一个很好的答案,您可以在其中进行更简单的查询:
select E'test line 1\ntest line 2';