IF EXISTS (SELECT 'x' FROM sysobjects WHERE name = 'Upper_Bound')
DROP FUNCTION Upper_Bound
GO
CREATE FUNCTION Upper_Bound
(@String as varchar(8000),
@Delimiter as varchar(8000))
RETURNS int
AS
BEGIN
/*
Author: Philip Leitch
Date: 2001
Purpose: Finds the last entry in a string based on a delimiter element. This is normally used with the split function to iterate through all entries.
Copyright: Philip Leitch 2001
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.
Liability: The developer assumes all liability when using this code.
*/
Declare @FoundIndex as int
declare @Length as int
declare @ReturnUBound as int
declare @RemainingString as varchar(8000)
set @FoundIndex = 0
set @ReturnUBound = 0
set @String = ltrim(rtrim(@string))
set @RemainingString = @String
set @FoundIndex = CHARINDEX (@Delimiter, @RemainingString )
while @FoundIndex <> 0
begin
set @ReturnUBound = @ReturnUBound + 1
set @RemainingString = right(@RemainingString, len(@RemainingString) - @FoundIndex)
set @FoundIndex = CHARINDEX (@Delimiter, @RemainingString )
end
RETURN @ReturnUBound
End
GO
-- =============================================
-- Example to execute function
-- =============================================
SELECT dbo.Upper_Bound('Hello, this is a test, to see if the split works, which I am sure it does, and if it does you will see a message saying, this function is working normally, okay?', ', ')