get Actual backup file name from the sql backup path

  • Hey everyone,

    sorry, i am trying to get the actual file name accurately from the path, what i currently have is:

    E:\Backup\Server1\Database1\Database1_131983519013.bak

    and want to get just:

    Database1_131983519013.bak

    trying to use trim or left etc. but finding it difficult as some other database names might be longer or shorter, so its random, any idea how I can get just the name?

     

    thanks in advance

  • This perhaps?

    DECLARE @MyString varchar(255)='E:\Backup\Server1\Database1\Database1_131983519013.bak'

    SELECT RIGHT(@MyString, CHARINDEX('\', REVERSE(@MyString)) - 1)
  • If you know the folder path in advance, you can simply remove it to get the file name:

    DECLARE @BackupFilePath VARCHAR(300) = 'E:\Backup\Server1\Database1\Database1_131983519013.bak';

    SELECT
    @BackupFilePath
    , FileName = REPLACE(@BackupFilePath, 'E:\Backup\Server1\Database1\', '');

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Thank you sir, worked perfectly 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply