471,090 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,090 software developers and data experts.

INSTR Function in MSSQL

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
Jul 20 '05 #1
1 25650
Lookup CHARINDEX and PATINDEX in Books Online.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Penn Markham | last post: by
6 posts views Thread by Chris Calzaretta | last post: by
12 posts views Thread by rodchar | last post: by
6 posts views Thread by Brian | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.