Given that SQL Server pre-2016 lacks a String Splitting function, i'm using the following user-defined function, which splits a string to multiple rows.
SQL
ALTERFUNCTION[dbo].[SplitStringToValues](@valueinnvarchar(max),@delimiterchar(1)=',')RETURNS@dataoutTABLE(datanvarchar(100))ASBEGINDECLARE@chrindINTDECLARE@Piecenvarchar(100)SELECT@chrind=1WHILE@chrind0BEGINSELECT@chrind=CHARINDEX(@delimiter,@valuein)IF@chrind0SELECT@Piece=LEFT(@valuein,@chrind-1)ELSESELECT@Piece=@valueinINSERT@dataoutVALUES(CAST(@PieceASVARCHAR))SELECT@valuein=RIGHT(@valuein,LEN(@valuein)-@chrind)IFLEN(@valuein)=0BREAKENDRETURNENDGO
What I do need to, however, is split the string on the current row (i.e. Value In, Comma 1, Comma 2, Comma 3 etc.) so I can output the results to a CSV.
Clearly the...