INSTR Function in MSSQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Icaro

    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(@strS ource, @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

    #2
    Re: INSTR Function in MSSQL

    Lookup CHARINDEX and PATINDEX in Books Online.

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


    Comment

    Working...