Given SQL Server 2008, I have written a simple find in string function as follows:
ALTER FUNCTION [dbo].[FindInString]
(
@FindText VARCHAR(255),
@TextSource VARCHAR(512)
)
RETURNS INT
AS
BEGIN
DECLARE @Result INT
SET @Result = 0
SELECT @Result = CHARINDEX(@FindText, @TextSource)
RETURN @Result
END
The complexity of the find function may change in the future, which is why I wanted to encapsulate it in a function.
Now, when I only have one matching record in a table, this works:
SELECT @FindCount = dbo.FindInString('somestring', (SELECT TableSearch FROM Segments WHERE CID=22793))
However, when the select statement returns more than one, it makes sense as to why an error is thrown.
like to know is what I need to do to still have this work as a simple call, as above?
I only need to know if there is one match (I just need to know if @FindCount > 0), and I’m guessing some sort of a loop may be required, but would like to keep this as simple as possible.
Thanks.
You can use aggregate functions and one select:
Just take care with this, as
FindInStringwill fire for each row, which can significantly reduce query performance. In this case, it’s the only way to solve your problem, but just beware of the troubles that could arise.