且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

postgresql - 替换文本字段中字符串的所有实例

更新时间:2023-02-23 09:37:54

你想使用 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.