April 27, 2012 at 7:46 am
Hi, How do I make this function to handle null or empty values
I do filter my other stored procedure data based on data received from this function, but it fails when there is empty or null value . Thanks.
Stored proc piece
-----------------
Where
(p.product IN (SELECT Item FROM dbo.brakstring(@product, ',')) Or @product IS NULL)
------FUNCTION
ALTER FUNCTION [dbo].[brakstring](@Text varchar(max),@Delimiter varchar(2)= ' ')
RETURNS @Strings TABLE
(
Position int IDENTITY PRIMARY KEY,
Item varchar(max)
)
AS
BEGIN
DECLARE @index int
SET @index = -1
WHILE (LEN(@Text) > 0)
BEGIN
SET @index = CHARINDEX(@Delimiter,@Text)
IF (@Index = 0) And (LEN(@Text) > 0)
BEGIN
INSERT INTO @Strings VALUES (@Text)
BREAK
END
IF (@Index > 1)
BEGIN
INSERT INTO @Strings VALUES (LEFT(@Text,@Index - 1))
SET @Text = RIGHT(@Text, (LEN(@Text) - @index))
END
ELSE
SET @Text = RIGHT(@Text, (LEN(@Text) - @index))
END
RETURN
END
April 27, 2012 at 7:51 am
You should read the article in my signature about splitting strings. It will blow this away for performance and it will explain how to use it.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 29, 2012 at 4:51 pm
Oddly enough, I'll second that recommendation. 😀
--Jeff Moden
Change is inevitable... Change for the better is not.
April 30, 2012 at 3:06 am
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply