How to split a comma seperated value?
Many times we need to write T-SQL statements to split a comma seperated value, however string is not necessarily to be comma seperated, it can be seperated by any delimiter e.g. comma (,), @, &, ; etc.
How to use Multi-valued Parameters of SSRS report in a Stored procedures?
One more question comes around, how to use a multi valued parameter of SSRS report in a Stored Procedure to filter report data? I am sure you can't use a multi valued parameter directly in T-SQL code without spliting multiple values, if you do so without spliting values, SPROC will throw an error.
To find the answer of above questions, you create a user defined function using below T-SQL code:
/**********************************************
CREATED BY HARI
PURPOSE : To split any multivalued string
seperated by any delimiter into multiple rows
***********************************************/
CREATE FUNCTION [dbo].[SplitMultivaluedString]
(
@DelimittedString [varchar](max),
@Delimiter [varchar](1)
)
RETURNS @Table Table (Value [varchar](100))
BEGIN
DECLARE @sTemp [varchar](max)
SET @sTemp = ISNULL(@DelimittedString,'')
+ @Delimiter
WHILE LEN(@sTemp) > 0
BEGIN
INSERT INTO @Table
SELECT SubString(@sTemp,1,
CharIndex(@Delimiter,@sTemp)-1)
SET @sTemp = RIGHT(@sTemp,
LEN(@sTemp)-CharIndex(@Delimiter,@sTemp))
END
RETURN
END
GO
/* How to use this function:
-----------------------------------------
SELECT * FROM [dbo].[SplitMultivaluedString] ('1,2,3,4', ',')
SELECT * FROM [dbo].[SplitMultivaluedString] ('1;2;3;4', ';')
SELECT * FROM [dbo].[SplitMultivaluedString] ('Hari#Thomas','#')
*/
Great function reduced the work drastically
ReplyDelete