SQL Server column comma-separated string and reversal

Source: Internet
Author: User

Select  STUFF ((select  ',' + modifyby                from    dbo.tbl_stationprofile              for                XML PATH (")              11") as Name    

Comma-separated string-to-table

Use [Mbg3sdb]go/** * * * object:userdefinedfunction [dbo].    [Getidlist] Script DATE:2017/9/12 10:13:38 * * * * **/SET ansi_nulls ongoset quoted_identifier Ongo-- =============================================--Description: Converting a string to a list (@InStr: String, @sp: delimiter)-- =============================================ALTER FUNCTION [dbo]. [Getidlist] (@InStr VARCHAR (max)="', @sp CHAR (1)=',') RETURNS @TempTable TABLE ([ID] VARCHAR ( -) ) Asbegin IF @InStr is NULL OR RTrim (LTrim (@InStr))="'RETURN DECLARE @FirstID VARCHAR ( -), @SpIndexintIF (@sp is NULL OR @sp="') SET @sp=','While @InStr<>"'BEGIN SET @SpIndex=CHARINDEX (@sp, @InStr) IF (@SpIndex=1) BEGIN SET @InStr=substring (@InStr,2, Datalength (@InStr)) CONTINUE END ELSE IF (@SpIndex>1) BEGIN SET @FirstID=substring (@InStr,1, @SpIndex-1) SET @InStr=substring (@InStr, @SpIndex +1, Datalength (@InStr) [Email protected]+1) END ELSE BEGIN SET @FirstID=@InStr SET @InStr="'end INSERT into @TempTable SELECT @FirstID END returnend/** Example SELECT * FROM dbo. Getidlist (' 5#6#7 ', ' # ') **/

Create your own table function and try it yourself. Not at the bottom.

SQL Server column comma-separated string and reversal

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.