How to Parse the Filename from the Whole Path in Oracle


Parsing the Filename from the Whole Path

Let’s look at a real example from the life of a DBA. Suppose you want to extract only the filename from dba_data_files without the path name; you could use the following SQL.

Here the INSTR function is nested inside a SUBSTR function. Single-row functions can be nested to any level. When functions are nested, the innermost function is evaluated first.

The INSTR function is used to find the character position where the last \ appears in the filename string (looking for the first occurrence from the end). The position ahead of that is passed into the SUBSTR function as the start position (hence the +1).


SELECT file_name,
SUBSTR(file_name, INSTR(file_name,’\’, -1,1)+1) Name
FROM dba_data_files;

FILE_NAME                                       NAME
----------------------------------------        -------------
C:\ORACLE\ORADATA1\MYNEWDB\USERS01.DBF          USERS01.DBF
C:\ORACLE\ORADATA1\MYNEWDB\UNDOTBS01.DBF        UNDOTBS01.DBF
C:\ORACLE\ORADATA2\MYNEWDB\SYSAUX01.DBF         SYSAUX01.DBF
C:\ORACLE\ORADATA3\MYNEWDB\SYSTEM01.DBF         SYSTEM01.DBF
C:\ORACLE\ORADATA3\MYNEWDB\EXAMPLE01.DBF        EXAMPLE01.DBF

To perform the same operation on Unix or Linux databases, replace \ in the INSTR function with /; because / is used on Linux/Unix to separate directories.

Let’s review another example using the Linux or Unix platform. Suppose you want to find out all the file systems (mount points) used by your database for storing data files; you could use the following SQL:

SELECT DISTINCT
SUBSTR(file_name, 1, INSTR(file_name,’/’, 1,2)-1) Mount_Point
FROM dba_data_files;
MOUNT_POINT
-----------------------------
/oradata1
/oradata2
/oradata3

In this example, you started looking for the second occurrence of / using the INSTR function and used SUBSTR to extract only the characters from 1 through the location before the second occurrence of / in the filename
(hence the –1).

Comments

Back To Top

Popular posts from this blog

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables

How to Drop or Remove or Decommission a Database in Oracle