postgresql - 替换文本字段中字符串的所有实例 [英] postgresql - replace all instances of a string within text field

查看:28
本文介绍了postgresql - 替换文本字段中字符串的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 postgresql 中,如何替换数据库列中字符串的所有实例?

In postgresql, how do I replace all instances of a string within a database column?

例如,我想用 dog 替换 cat 的所有实例.

Say I want to replace all instances of cat with dog, for example.

最好的方法是什么?

推荐答案

你想使用 postgresql 的 替换函数:

You want to use postgresql's replace function:

replace(string text, from text, to text)

例如:

UPDATE <table> SET <field> = replace(<field>, 'cat', 'dog')

但是请注意,这将是字符串到字符串的替换,因此类别"将变为dogegory".regexp_replace 函数可以帮助您为要替换的内容定义更严格的匹配模式.

Be aware, though, that this will be a string-to-string replacement, so 'category' will become 'dogegory'. the regexp_replace function may help you define a stricter match pattern for what you want to replace.

这篇关于postgresql - 替换文本字段中字符串的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆