gpt4 book ai didi

sql - SQL中如何替换数字

转载 作者:行者123 更新时间:2023-11-29 11:42:31 25 4
gpt4 key购买 nike

我的数据库有很多相似的描述,我想把它们归为一组,但是因为编号不同,所以没有归为一组。那么有什么方法可以掩盖数字并使描述相同。

我们可以在 Excel 或 Notepad++ 中使用查找和替换来做到这一点,所以无论如何它在 SQL 中都是可能的。我知道我们可以使用函数在 SQL 中替换

REPLACE('column', 'new input', 'to be replaced')

但是如何为正则表达式执行此操作,因为数字可以是任意组合。

我正在使用 PostgreSQL。

一些输入:-

sample input description 123
sample input description 456
this is another description 678
this is another description 999

我想将它们转换为:-

sample input description xxx
sample input description xxx
this is another description xxx
this is another description xxx

数字可以在任何地方。

我在 redshift 上做。

最佳答案

你会用

regexp_replace(col, '[[:digit:]]+', '#')

为了用一个 # 替换任意数量的数字。

Rextester 演示:http://rextester.com/BFSP36237

如果一个字符串中可以出现多个数字,则使用标志 'g':

regexp_replace(col, '[[:digit:]]+', '#', 'g')

Rextester 演示:http://rextester.com/WHTJ51233

关于sql - SQL中如何替换数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com