0

在 Postgres 中,我试图将一个字符串附加到单个字段中每行数据的末尾。

例如,我有以下名为personal_details的数据库表:

+-------+--------+-----------------------------------------------------+
|  name |   age  |                       details                       |
+-------+--------+-----------------------------------------------------+
| Dave  |   67   |  This is some text made by Dave                     |  <-- Note the next sentence is on the next line
|       |        |  He is a dentist and works five days a week         |
|       |        |                                                     |  <-- Note there is a blank line between these two sentences 
|       |        |  He enjoys sports and loves burgers                 |
|       |        |  He hates pizza                                     |
+-------+--------+-----------------------------------------------------+
| Paul  |   34   |  This is some text from a different person          |
|       |        |                                                     | 
|       |        |  Paul is always very happy                          |
|       |        |  He loves dogs                                      | 
+-------+--------+-----------------------------------------------------+
| Lucy  |   27   |  Lucy is the only girl in this database table       |
|       |        |  She likes coffee                                   |
|       |        |                                                     |
|       |        |  She does karate at the weekend                     |
+-------+--------+-----------------------------------------------------+

问题(为了解释我的问题而大大简化):

我正在使用该personal_details表创建一个personal_details_view使用以下简单 SQL 调用的视图:

SELECT personal_details.name, personal_details.age, peronsal_details.details 
FROM personal_details

但是在这个视图中,在上Details表的字段中,我想根据是单换行还是双换行,在每个句子的末尾插入一个特定的字符串。

例如,在视图中Detail,Dave、Paul 和 Lucy 的字段将变为:

---------------------------------------------------------
This is some text made by Dave</text></line><line><text>                                               <-- to signify a SINGLE newline followed this
He is a dentist and works five days a week</text></line><line><text></text></line><line><text>         <-- to signify a DOUBLE newline followed this
He enjoys sports and loves burgers</text></line><line><text>
He hates pizza
--------------------------------------------------------

---------------------------------------------------------
This is some text from a different person</text></line><line><text></text></line><line><text>          <-- to signify a DOUBLE newline followed this
Paul is always very happy</text></line><line><text>                                                    <-- to signify a SINGLE newline followed this 
He loves dogs
---------------------------------------------------------

---------------------------------------------------------
Lucy is the only girl in this database table</text></line><line><text>                                 <-- to signify a SINGLE newline followed this 
She likes coffee</text></line><line><text></text></line><line><text>                                   <-- to signify a DOUBLE newline followed this
She does karate at the weekend
---------------------------------------------------------                 

</text></line><line><text>表示这句话后面跟着一个新行

</text></line><line><text></text></line><line><text>表示这句话后面有一个双新行。

我可能忽略了一种明显的方法,但我看不到这样做的合乎逻辑的方法。

4

1 回答 1

2

在这种情况下,一个换行符或两个换行符之间没有真正的区别

这能完成工作吗?

select replace(details, E'\n', '</text></line><line><text>'||E'\n') from personal_details

编辑:在阅读了您对所需结果的最新编辑后,我还建议进行双重替换:

select replace(
  replace(details, E'\n\n', '</text></line><line><text>'||E'\n'),
E'\n', '</text></line><line><text>'||E'\n')
from personal_details

首先运行的内部替换,将所有双换行符替换为您想要的额外字符串一次,加上一个换行符,

而外部替换进一步在遇到的所有换行符中添加所需的字符串。

如果你想在你的文件中输出单行,你可以删除最后一个||E'\n'外部替换

于 2013-10-31T13:31:50.600 回答