Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 use function that split delimeted string to search

Author  Topic 

magmo
Aged Yak Warrior

558 Posts

Posted - 2014-09-10 : 15:59:53
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...


gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-09-11 : 09:29:58
see here: www.sqlservercentral.com/articles/Tally+Table/72993/
Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2014-09-11 : 16:11:47
[code]select m.*
from MyTable m
inner join (SELECT ReturnValue FROM SplitDelimiterString(@ListOfIDs, @DelimeterCharachter)) s
on m.textColumn like '%' + s.ReturnValue + '%'[/code]I don't really know what the name of the column returned from the function is so you'll need to replace "ReturnValue" with the actual name.

HTH



Too often we enjoy the comfort of opinion without the discomfort of thought. - John F. Kennedy
Go to Top of Page
   

- Advertisement -