SQL查询以更改包含文件路径的记录中的文件扩展名?

SQL查询以更改包含文件路径的记录中的文件扩展名?,sql,oracle,Sql,Oracle,给定一个SQL表table和一列path,如何修改/dir/subdir/file.aaa=>/dir/subdir/file.bbb等值,例如,只修改文件扩展名,而不必将特定的文件/路径硬编码到我的查询中?如果该列只包含类似于文件的值,以下路径将起作用: update the_table set path = replace(path, '.aaa', '.bbb') where path like '%.aaa'; 请注意,这也会将类似/dir/subdir.aaa/file.aaa

给定一个SQL表
table
和一列
path
,如何修改
/dir/subdir/file.aaa
=>
/dir/subdir/file.bbb
等值,例如,只修改文件扩展名,而不必将特定的文件/路径硬编码到我的查询中?

如果该列只包含类似于文件的值,以下路径将起作用:

update the_table
   set path = replace(path, '.aaa', '.bbb')
where path like '%.aaa';
请注意,这也会将类似
/dir/subdir.aaa/file.aaa
的值更新为
/dir/subdir.bbb/file.bbb

另一个选项是使用正则表达式:

update foo 
  set file_path = regexp_replace(file_path, '\.aaa$', '.bbb', 1, 0, 'i')
where lower(file_path) like '%.aaa';
似乎非常适合:


请参阅一些测试

IIRC,这是区分大小写的,有没有简单的方法?e、 g..txt和.txt都应该匹配
with t as (select '/dir/subdir/file.aaa' as path from dual
           union all select '/dir/subdir.aaa/file.aaa' from dual)

select regexp_replace(path, '[.][^.]*$', '.bbb') path
--          ^^^^             ^^^^^^^^^    ^^^^
--        replace     last dot-whatever   by the "right" extension
from t
where path like '%.aaa'
--               ^^^^^
--    only for path ending with the "wrong" extension