0

I am trying to trim a long file name and just want to get the last characters of that file name which is after the last forward slash (\) and I also want to eliminate the .xlsx at the end of the file name as well.

Original:

sasa\sasas\sasas\1234_IASS.xlsx

Expected Output:

1234_IASS
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
Minhal
  • 85
  • 2
  • 17

6 Answers6

1

You can try this it will work as said in the comment the file extension are fixed.

SELECT 
Replace(
    RIGHT('\' + RTRIM('sasa\sasas\sasas\1234_IASS.xlsx'), CHARINDEX('\', REVERSE('\' + RTRIM('sasa\sasas\sasas\1234_IASS.xlsx'))) - 1)
          ,'.xlsx', '') 
as FileName

You can find the live example here.

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
1

Your post's title is misleading, TRIM is performed in sql-server to remove whitespace either by RTRIM or LTRIM or a combination of the two, what you are trying to do is get a substring out of your example string, I am providing a solution which uses a combination of REVERSE, SUBSTRING and CHARINDEX, this answer is good for if you ever need to do this for different file extensions:

DECLARE @test varchar(255) = 'sasa\sasas\sasas\1234_IASS.xlsx';
DECLARE @lastOccurance INT = CHARINDEX('\', REVERSE(@test)); --Last occurence of the slash character to denote the end of the directory name or what not
DECLARE @lastPeriod INT = CHARINDEX('.', REVERSE(@test)); --This is the last occurence of the period, denoting the file extension
SET @lastOccurance = LEN(@test) + 1 - @lastOccurance;
SET @lastPeriod = LEN(@test) + 1 - @lastPeriod;
SELECT SUBSTRING(@test, @lastOccurance + 1, @lastPeriod - (@lastOccurance + 1));
Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
1

You say your directory is the same and the extension is always the same? Replace [path] with your table column name:

select replace(replace([path],'sasa\sasas\sasas\',''),'.xlsx','')
gbeaven
  • 1,522
  • 2
  • 20
  • 40
1

Your comments state that both your file path and file extension are constant. If the number of characters in your file is also constant the simplest solution is to use SUBSTRING.

SELECT SUBSTRING(YourColumn, 18, 9)
FROM YourTable

If the number of characters is changing, a more robust solution is to use RIGHT to extract the file name and REPLACE to remove the file extension.

SELECT REPLACE(RIGHT(YourColumn, LEN(YourColumn) - 17), '.xlsx', '')
FROM YourTable

If you need a more dynamic solution, you can first extract the filename as shown.

SELECT RIGHT(YourColumn, CHARINDEX('\', REVERSE(YourColumn)) - 1)
FROM YourTable

You can then combine this with REPLACE as before to remove the extension.

SELECT REPLACE(RIGHT(YourColumn, CHARINDEX('\', REVERSE(YourColumn)) - 1), '.xlsx', '')
FROM YourTable
Derrick Moeller
  • 4,808
  • 2
  • 22
  • 48
1
declare @x varchar(100) = 'sasa\sasas\sasas\1234_IASS.xlsx'

declare @filename varchar(max) = reverse(substring(reverse(@x), 1, charindex('\', reverse(@x))-1 ))
select substring(@filename, 1, charindex('.', @filename)-1)
Daniel N
  • 1,122
  • 1
  • 8
  • 14
1

If you want to remove extensions from filename then you can try this:

UPDATE TableName
SET FileName = REVERSE(SUBSTRING(REVERSE(FileName), 
                       CHARINDEX('.', REVERSE(FileName)) + 1, 999))
Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42