How to Get Only File Name from Full File Path

In our database we normally save the full path of files (i.e. photos, drawings etc.).  On many occasions I have to show only the actual file name.  Previously I was using PATINDEX and REVERSE for that, and I was also checking to see if the number of ‘\’s in the full path was zero or not.  I have now found a better way of doing this job.

SELECT
RIGHT('\' + MYFILEPATH, CHARINDEX('\', REVERSE(MYFILEPATH)) - 1)
FROM MYTABLE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s