如何从路径中提取根目录
How to extract the root directory from a path
我正在尝试通过获取路由的第一个目录来创建正则表达式。
例如:
/home/oracle/file1.dbf
/home/oracle/file2.dbf
/usr/oracle/file3.dbf
/usr/oracle/file3.dbf
你应该return:
/home
/home
/usr
/usr
我试过以下查询:
SELECT substr(file_name,instr(file_name,'/')-1) as FILE_NAME
FROM dba_data_files;
语法是SUBSTR( string, start_position, substring_length )
so you want to start from the 1st character and the length is the position of the 2nd occurrence of the slash character which can be found using INSTR( string, substring, start_position, occurrence )
:
SELECT SUBSTR(
file_name,
1,
INSTR( file_name, '/', 1, 2 ) - 1
) AS root_directory
FROM dba_data_files;
我正在尝试通过获取路由的第一个目录来创建正则表达式。
例如:
/home/oracle/file1.dbf
/home/oracle/file2.dbf
/usr/oracle/file3.dbf
/usr/oracle/file3.dbf
你应该return:
/home
/home
/usr
/usr
我试过以下查询:
SELECT substr(file_name,instr(file_name,'/')-1) as FILE_NAME
FROM dba_data_files;
语法是SUBSTR( string, start_position, substring_length )
so you want to start from the 1st character and the length is the position of the 2nd occurrence of the slash character which can be found using INSTR( string, substring, start_position, occurrence )
:
SELECT SUBSTR(
file_name,
1,
INSTR( file_name, '/', 1, 2 ) - 1
) AS root_directory
FROM dba_data_files;