All of the above examples are known to intercept locations and lengths, and here are examples of unknown locations
2. function to intercept unknown position
A. Intercepting the string after the specified string (for example, intercepting the string following the http://)
Method One:
Declare @S1 varchar (100)
Select @S1 = ' http://www.xrss.cn '
Select Substring (@S1, CHARINDEX (' www ', @S1) +1,len (@S1))
* This can also be written here: Select Substring (@S1, CHARINDEX ('//', @S1) +2,len (@S1)) * *
------------------------------------
Display results: www.163.com
Note: The CHARINDEX function searches for strings that are case-insensitive, so charindex (' www ', @S1) can also be written as charindex (' www ', @S1)
Method Two: (similar to the method one)
Declare @S1 varchar (100)
Select @S1 = ' http://www.xrss.cn '
Select Substring (@S1, PATINDEX ('%www% ', @S1) +1,len (@S1))
It can also be written here: Select Substring (@S1, PATINDEX ('%//% ', @S1) +2,len (@S1))
------------------------------------
Display results: www.163.com
The difference between function Patindex and Charindex is that the former can parameter some parameters and increase the function of the query.
The difference between function stuff and replace is that the former can specify a replacement range, while the latter is full range substitution
B. Intercepting the string after the specified character (for example, intercepting the filename in C:\Windows\test.txt)
Unlike a, when a search object is not one, you can use the above method to search only the first location
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