HiI 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 ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].[SplitDelimiterString] (@StringWithDelimiter VARCHAR(8000), @Delimiter VARCHAR(8))RETURNS @ItemTable TABLE (Item VARCHAR(8000))ASBEGIN 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 RETURNENDGO
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...