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

时间:2014-10-27 14:57:44

标签: sql oracle

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

2 个答案:

答案 0 :(得分:1)

似乎非常适合regexp_replace

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

有关某些测试,请参阅http://sqlfiddle.com/#!4/d41d8/37017

答案 1 :(得分:0)

如果列仅包含结构类似于文件的值,则以下路径将起作用:

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';