MSSQL SQL Server below shares a novel method of string interception

Source: Internet
Author: User
Tags sql tutorial mssql

Original address: http://www.maomao365.com/?p=7307

Summary:previously, when splitting a string, it was handled in a way similar to the Split function, and the following shared a way of separating a regular string.that is: 1. Use the Replace function to turn the string into a compliant XML format string2. Get the specified string information using the Xml.value methodis as follows:Lab Environment: SQL Server R2
--Example:-----Get the contents of the brackets in the following string-----We can replace the following rule's string with replace with the XML-formatted string, and then get the relevant information using the XML selectDECLARE @stringInfo   VARCHAR(MAX)='1:[Cat Hut],2:[string split],3:[sql Tutorial Blog]'      , @q VARCHAR(3)=3;---Get information from the third separator Declare @xmlInfoXML;Set @stringInfo = '<xmlinfo keyId = "'+@stringInfo;Set @stringInfo = REPLACE(@stringInfo,'[','"Value="');Set @stringInfo = REPLACE(@stringInfo,'],','"/><xmlinfo keyId ="');Set @stringInfo = REPLACE(@stringInfo,']','"/>');Set @stringInfo = REPLACE(@stringInfo,':',"'); Set @xmlInfo = CONVERT(XML,@stringInfo); SELECT @xmlInfo  as [Xmlinfo],@xmlInfo. Value ('(/xmlinfo[@keyId =sql:variable ("@q")]/@value) [1]', 'varchar ($)');

MSSQL SQL Server below shares a novel method of string interception

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.