如何简化这个PostgreSQL?基本上我想检查字段是否结束
(1)
或仅(1)
,并替换并重复1-30中的数字。我认为它可以用某种方式用正则表达式完成,但我还没有工作。
UPDATE discogs.artist_meta
SET name = substr(name,0, strpos(name,' (1)'))
WHERE name LIKE '% (1)';
UPDATE discogs.artist_meta
SET name = substr(name,0, strpos(name,'(1)'))
WHERE name LIKE '%(1)';
UPDATE discogs.artist_meta
SET name = substr(name,0, strpos(name,' (2)'))
WHERE name LIKE '% (2)';
UPDATE discogs.artist_meta
SET name = substr(name,0, strpos(name,'(2)'))
WHERE name LIKE '%(2)';
答案 0 :(得分:2)
regexp_replace(target, regexp, replacewith)
function ' ?\(([1-9]|[12][0-9]|30)\)$'
''
。Regexp解释:
" ?" = optional space
\( = opening parenthesis (escaped)
([1-9]|[12][0-9]|30) = numbers 1-30
\) = closing parenthesis (escaped)
$ = end of content
关于你的评论:
update discogs.artist_meta
set name = regexp_replace(name, ' ?\(([1-9]|[12][0-9]|30)\)$', '')
where name ~ ' ?\(([1-9]|[12][0-9]|30)\)$'