给定一个文件路径,我试图在 Oracle PL/SQL 中分隔文件名和父目录。我想知道 PL/SQL 库中是否已经有可用的函数
问问题
1633 次
2 回答
3
没有真正的需要,如果您使用的是 10g 或更高版本,它只需要一些正则表达式技巧,这取决于您的操作系统,可能如下所示:
with filepaths as (
select '/home/oracle/somedir/blah.sql' as linux
, 'c:\home\oracle\somedir\blah.sql' as windows
from dual
)
select regexp_substr(linux, '[^/]+', 1
, regexp_count(linux,'/')) as linux
, regexp_substr(windows, '[^\]+', 1
, regexp_count(windows,'[^\]+')) as windows
from filepaths
;
LINUX WINDOWS
-------- --------
blah.sql blah.sql
于 2012-08-09T17:08:48.167 回答
1
INSTR 和 SUBSTR 字符串函数可用于执行此操作,如下所示:
WITH filepaths AS
(SELECT '/home/oracle/somedir/blah.sql' AS LINUX,
'c:\home\oracle\somedir\blah.sql' AS WINDOWS
FROM DUAL)
SELECT LINUX,
SUBSTR(LINUX, 1, INSTR(LINUX, '/', -1)) AS LINUX_PATH,
SUBSTR(LINUX, INSTR(LINUX, '/', -1)+1) AS LINUX_FILE,
WINDOWS,
SUBSTR(WINDOWS, 1, INSTR(WINDOWS, '\', -1)) AS WINDOWS_PATH,
SUBSTR(WINDOWS, INSTR(WINDOWS, '\', -1)+1) AS WINDOWS_FILE
FROM filepaths;
LINUX /home/oracle/somedir/blah.sql
LINUX_PATH /home/oracle/somedir/
LINUX_FILE blah.sql
WINDOWS c:\home\oracle\somedir\blah.sql
WINDOWS_PATH c:\home\oracle\somedir\
WINDOWS_FILE blah.sql
分享和享受。
于 2012-08-10T17:01:13.483 回答