Hi everybody,
I was looking for an equivalent ORACLE INSTR Function in MSSQL but I
donīt found it and I donīt know if it exist so I must to write it and
this is the code. Maybe it will be helful to you:
/************************************************** *************************
Description:
Looks for a string inside another string and returns an integer
that correspond to the position of first ocurrence.
Parameters:
Input:
- strSource. Contains the string where the functions look for the
other string
- strToFind. Contains the string to look for inside strSource
Salida:
- Integer value indicating the position of first occurrence of
strToFind in strSource
************************************************** *************************/
CREATE FUNCTION posSubString
(@strSource varchar(400),
@strToFind varchar(200)) RETURNS int
AS
BEGIN
DECLARE
@position int,
@maxPos int,
@longSubStr int,
@res int,
@strSub varchar(200)
SET @position = 0
SET @res = 0
SET @longSubStr = LEN(RTIRM(LTRIM(@strToFind)))
SET @maxPos = LEN(@strSource) - @longSubStr
WHILE (@position <= @strToFind)
BEGIN
SET @strSub = SUBSTRING(@strSource, @position, @longSubStr)
IF (@strToFind = @StrSub)
BEGIN
SET @res = @position - 1
RETURN @res
END
ELSE
SET @position = @position + 1
END
RETURN @res
END
Alonso