Gets the character after the last '/' of the URL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.