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