Technical Article

sp_CHARINDEX

,

Here's a stored procedure version of udf_CharIndex for SQL Server 7.

Sample Usage:
DECLARE @iPos INT
DECLARE @nRet INT

EXECUTE @nRet = sp_CharIndex 'W', 'Hello World', @nPos = @iPos OUTPUT

IF @iPos > 0
   PRINT 'found'
ELSE
   Print 'not found'

PRINT @iPos

  --Only uses on character.

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE sp_CharIndex(@sChar CHAR(1), @strKey VARCHAR(1024), @nPos INT OUTPUT)

AS
BEGIN
   /***
    *
    *  Date:   01/22/2002
    *  Auth:   Mike McWilliams <mike.mcwilliams@agedwards.com>
    *  Desc:   Returns the position of the character with case-sensitivity
    *
    ***/
--   DECLARE @nPOS INT
   DECLARE @nRet INT

   SET @nRet = 0
   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
  SET @nPos = @nRet
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating