sql - 更新包含换行符 '\n' 的文本列

标签 sql postgresql sql-update psql

我在尝试运行以下查询时遇到 PostgreSQL 的奇怪行为

update posts 
set content = replace(content, '\n', '<br>') 
where content is not null;

并且它不会对数据库中的数据执行任何操作。我 **尝试手动提交(包括尝试从 psql 运行此查询)** 以及将 DBeaver/pgAdmin 设置为 AUTOCOMMIT 但无济于事。

结果告诉我已更新 37 行,但没有更改。如果我尝试提交,它会告诉我 0 行受到影响。

我根本没有触发器,所以这是不可能的。

我在这里遗漏了什么吗?

最佳答案

在文字前使用e:

update posts 
set content = replace(content, e'\n', '<br>') 
where content is not null
-- or better
-- where content like e'%\n%'

来自文档 (String Constants with C-Style Escapes):

An escape string constant is specified by writing the letter E (upper or lower case) just before the opening single quote.

关于sql - 更新包含换行符 '\n' 的文本列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359796/

相关文章:

c# - 在 EF Core 中更新数据的更好方法是什么

sql - 将同一个表中的多个列组合成 1 个更长的唯一列?

sql - 从选择查询的列数据中删除特定字符

json - Postgresql - 在从 JSON 和表中选择值时插入到两个表中

postgresql - 我怎样才能得到 Postgres 端口

MySQL varchar 默认为 0 而不是空字符串

mysql - 如何根据条件检索所有记录

mysql - 如何添加列表或字典作为 SQL 表条目?

ruby-on-rails - 日志数据分析 : Choice of Database

sql - 如何使用分组依据的结果更新表