Question with Development.

  • If I have a string like W:\ABC\F1.bak, is there any way to capture just W:\ABC, it is not necessary that it could be just W:\ABC, it could be W:\ABCD as well, the main intention is that if there are 2 backslashes in the entire string, I would like to capture the entire text before the second backslash or in other words i want to caapture the entire string except .bak, .trn and so on.

    Any help would be Highly Appreciated.

  • This will return the string before the last backslash. It'll work if there's more than 2.

    DECLARE @STR VARCHAR(200) = 'W:\ABC\F1.bak'

    SELECT LEFT(@Str, LEN(@Str) - CHARINDEX('\', REVERSE(@Str)))

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 2 posts - 1 through 1 (of 1 total)

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