XML / SQL - 在各个字段的每行末尾添加一个字符串

时间:2013-10-31 13:01:24

标签: sql xml postgresql xslt newline

我有一个问题,我希望有人可以提供帮助...

在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字段中,我想在每个句子的末尾插入一个特定的字符串,具体取决于是否有一个换行符或一个双新行。

例如,在查看Dave的Detail字段时,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> 表示此句话后面有一个双重新行。

我可能会忽视一种显而易见的方法,但是我看不出这样做的合理方式吗?

非常感谢您对此的帮助,非常感谢

1 个答案:

答案 0 :(得分: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'