SQL Server String Processing

Source: Internet
Author: User

DECLARE @str VARCHAR( -)SET @str='ap-fqc-2014072300004'--gets the position of the first occurrence of the specified characterSELECT PATINDEX('%-%',@str)--returns: 3--gets the string before the position of the first occurrence of the specified characterSELECT SUBSTRING(@str,1,CHARINDEX('-',@str)-1)--back to: AP--gets the position of the last occurrence of the specified characterSELECT LEN(@str)-CHARINDEX('-',REVERSE(@str))+1  --returns: 7--gets the character between the first occurrence of the specified character and the position of the second occurrenceSELECT SUBSTRING(@str,PATINDEX('%-%',@str)+1,LEN(@str)-CHARINDEX('-',REVERSE(@str))+1-PATINDEX('%-%',@str)-1)--return: FQC--gets all strings after the position where the specified character last appearsSELECT REVERSE(SUBSTRING(REVERSE(@str),1,CHARINDEX('-',REVERSE(@str))-1))--return: 2014072300004
Related Article

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.