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