Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

INSTR Function in MSSQL

2 views
Skip to first unread message

Icaro

unread,
Jan 22, 2004, 9:34:26 AM1/22/04
to
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

David Portas

unread,
Jan 22, 2004, 9:56:29 AM1/22/04
to
Lookup CHARINDEX and PATINDEX in Books Online.

--
David Portas
------------
Please reply only to the newsgroup
--


0 new messages