Development project, encountered a problem:' abc/def/jkl ', get the last '/' after all the characters ' JKL ', because the number of special characters '/' is not fixed, how to use the TSQL statement to quickly implement this requirement?
The idea is simple: because the position of the last special character '/' is uncertain, make a move, use the reverse function, convert the last special character '/' to the first special character, and get all the characters before the first special character '/'.
SCRIPT1, for cases where a null character is not terminated with ' \ ' or terminated with ' \ '
Declare @ExpressionToSearch varchar(Max)Set @ExpressionToSearch='/EEABC/DEF/ABC/JKL'--Set @ExpressionToSearch = ' EEABC/DEF/ABC/JKL '--Select reverse (@ExpressionToSearch)Select Right(@ExpressionToSearch, IIF (charindex('/',Reverse(@ExpressionToSearch),1)=0, Len(@ExpressionToSearch), charindex('/',Reverse(@ExpressionToSearch),1)-1) )
SCRIPT2, if you end with a '/', take the last two '/' character string, similar to ' jkl ' from the string ' abc/def/jkl/'
Declare @ExpressionToSearch varchar(Max)Set @ExpressionToSearch='/eeabc/def/abc/jkl/'--Set @ExpressionToSearch = ' EEABC/DEF/ABC/JKL '--Select reverse (@ExpressionToSearch)Select Left(Strtosearch,Len(Strtosearch)-charindex('/',Reverse(Strtosearch),1)) from (Select Right(@ExpressionToSearch, IIF (charindex('/',Reverse(@ExpressionToSearch),2)=0, Len(@ExpressionToSearch), charindex('/',Reverse(@ExpressionToSearch),2)-1) )) asT (Strtosearch)
Gets the character after the last '/' of the URL