SQL table and string Mutual transfer

Source: Internet
Author: User

--table to StringStuff((Select Top  - ','+ cast(QQ as varchar( -)) fromDl_qqOrder  byQq forXML Path ("')),1,1,"')

--String to tableCREATE FUNCTIONSplit (@Text NVARCHAR(4000),@Sign NVARCHAR(4000))  RETURNS  @tempTable TABLE(IDINT IDENTITY(1,1)PRIMARY KEY,[VALUE] NVARCHAR(4000))   as  BEGIN      DECLARE @StartIndex INT                --where to start looking    DECLARE @FindIndex  INT                --location found    DECLARE @Content    VARCHAR(4000)--the value found    --initialize a number of variables    SET @StartIndex = 1 --The find position of a string in T-SQL starts at 1    SET @FindIndex=0            --start loop lookup string comma     while(@StartIndex <= LEN(@Text))      BEGIN          --Find String function CHARINDEX The first argument is the string to find        --The second parameter is where to find the string        --The third parameter is where you start looking        --The return value is where the string was found        SELECT @FindIndex = CHARINDEX(@Sign,@Text,@StartIndex)          --Judging if there's no found. return 0        IF(@FindIndex =0 OR @FindIndex  is NULL)          BEGIN              --if we don't find them , we're done.            SET @FindIndex = LEN(@Text)+1          END          --Intercept string function SUBSTRING The first argument is the string to intercept        --The second parameter is the starting position        --The third parameter is the length of the Intercept        --@[email protected] indicates the position to find-the position to start with = length to intercept        --LTRIM and RTRIM are whitespace functions that remove the left and right side of a string        SET @Content = LTRIM(RTRIM(SUBSTRING(@Text,@StartIndex,@FindIndex-@StartIndex)))          --Initialize the location of the next lookup        SET @StartIndex = @FindIndex+1          --Insert the value you are looking for in the table type that you want to return        INSERT  into @tempTable([VALUE])VALUES(@Content)       END      RETURN  END SELECT *  fromDbo. Split ('a,b,c,d,e,f,g',',')

SQL table and string Mutual transfer

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.