【问题标题】:In postgres, how would I replace or add a prefix/suffix to all the selected text in a column?在 postgres 中,如何为列中的所有选定文本替换或添加前缀/后缀?
【发布时间】:2021-06-09 20:09:54
【问题描述】:

我在 postgres 数据库上使用 DBeaver,我想编辑列中的一些文本。

从表 'user',列 'name',我选择了名为 'James' 和 'Bond' 的条目。

SELECT * FROM public."user" WHERE name IN ('James', 'Bond') 

向这两个条目添加“007”前缀以便“James”和“Bond”分别变为“007James”和“007Bond”的好查询是什么?

另外,在我更新名称以显示为“007James”和“007Bond”后,如何替换前缀并将这些条目更改为“008James”和“008Bond”?

谢谢

【问题讨论】:

  • 使用字符串连接|| 如:SELECT '007' || name FROM public."user" WHERE name IN ('James', 'Bond')
  • 不,我想将“007”添加到这两个字符串中。

标签: sql postgresql


【解决方案1】:

你的问题的第一部分是这样的:

UPDATE public."user" SET name = '007' || name WHERE name IN ('James', 'Bond');

所以,将007 与当前的name 值连接起来

第二部分,使用REPLACE函数

UPDATE public."user" SET name = REPLACE(name, '007', '008');

【讨论】:

    猜你喜欢
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2016-03-07
    • 2017-08-26
    • 2010-11-05
    • 2015-12-05
    相关资源
    最近更新 更多