Hi
I try to use the following function in a Query.
USE [TestDB]
GO
/****** Object: UserDefinedFunction [dbo].[SplitDelimiterString] Script Date: 2014-09-10 09:05:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[SplitDelimiterString] (@StringWithDelimiter VARCHAR(8000), @Delimiter VARCHAR(8))
RETURNS @ItemTable TABLE (Item VARCHAR(8000))
AS
BEGIN
DECLARE @StartingPosition INT;
DECLARE @ItemInString VARCHAR(8000);
SELECT @StartingPosition = 1;
--Return if string is null or empty
IF LEN(@StringWithDelimiter) = 0 OR @StringWithDelimiter IS NULL RETURN;
WHILE @StartingPosition > 0
BEGIN
--Get starting index of delimiter .. If string
--doesn't contain any delimiter than it will returl 0
SET @StartingPosition = CHARINDEX(@Delimiter,@StringWithDelimiter);
--Get item from string
IF @StartingPosition > 0
SET @ItemInString = SUBSTRING(@StringWithDelimiter,0,@StartingPosition)
ELSE
SET @ItemInString = @StringWithDelimiter;
--If item isn't empty than add to return table
IF( LEN(@ItemInString) > 0)
INSERT INTO @ItemTable(Item) VALUES (@ItemInString);
--Remove inserted item from string
SET @StringWithDelimiter = SUBSTRING(@StringWithDelimiter,@StartingPosition +
LEN(@Delimiter),LEN(@StringWithDelimiter) - @StartingPosition)
--Break loop if string is empty
IF LEN(@StringWithDelimiter) = 0 BREAK;
END
RETURN
END
GO
I can use the function to pass a number of delimeted ID's like this..
'1;4;223;441'
WHERE ID IN (SELECT * FROM SplitDelimiterString(@ListOfIDs, @DelimeterCharachter))
This works fine if I would like to find rows based on ID's, my question is, if I would like to use the same function but for nVarchar columns. What do I need to change because it doesnt work doing this..
I basically want to pass in several search parameters in a delimeted string and find matches in x number of columns.
WHERE textColumn = (SELECT * FROM SplitDelimiterString(@ListOfIDs, @DelimeterCharachter))
Or
WHERE textColumn LIKE (SELECT * FROM SplitDelimiterString(@ListOfIDs, @DelimeterCharachter))
Hope anyone can help me out...