Question

once of the column I am working with contains directory path for the file imported. However, I do not want the whole directory path. I just want the file name.

What I am doing:

I am trying to use Charindex function to locate following: '\'. I've also tried to combine Max, substring, right, left, and etc. functions to get what I am looking for, but no luck.

Can someone please guide me to the right path please?

 SELECT 
substring(batch_ID, max(charindex('\', batch_ID )), len(batch_ID) - max(charindex('\'))),
Était-ce utile?

La solution

If your'e trying to select the filename from a full file path, this works in MS SQL:

SELECT RIGHT(batch_ID ,CHARINDEX('\',REVERSE(batch_ID ))-1)

An Aggregate function like MAX() can't be used in the context you were wanting, because it is designed to return a value from a series of rows.

To break it down:

REVERSE('C:\Users\xyz122\Desktop\PF\import\12-13\COMRECOP.xml')

Gets you: LMX.POCERMOC\31-21\tropmi\FP\potkseD\221zyx\sresU\:C

Then CHARINDEX finds the \ in the 13th position of the reversed string.

Telling you that you want the last 13 characters of the string, which we get with RIGHT(), but that includes the '\', so we subtract 1 from the 13 to get just the filename.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top