Technical Article

Case sensitive CHARINDEX Function

,

Microsoft missed the code boat on this one.  Use this function to find the true index of set of characters.

ALTER FUNCTION udf_CharIndex(@sChar CHAR(1), @strKey VARCHAR(94)) 
RETURNS INT
AS
BEGIN
   /***
    *
    *  Date:   01/22/2002
    *  Auth:   Mike McWilliams <mikemcw@4segway.biz>
    *  Desc:   Returns the position of the character with case-sensitivity
    *
    ***/
   DECLARE @nPOS INT
   DECLARE @nRet INT

   SET @nRet = -1
   SET @nPOS = 1

   WHILE @nPOS < LEN(@strKey) + 1
      BEGIN
         IF ASCII(@sChar) = ASCII(SUBSTRING(@strKey, @nPos,1))
            BEGIN
               SET @nRet = @nPos
               BREAK
            END
         ELSE
            SET @nPos = @nPos + 1

      END

  RETURN(@nRet)
END

Rate

1 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

1 (2)

You rated this post out of 5. Change rating