-- See how many jewels are among the stones
DECLARE @S VARCHAR(1024) 	=	'AaBbCcAa'
,	@J VARCHAR(1024)	=	'aA';

DECLARE @SLEN	INT = len(@S);
DECLARE @JLEN	INT = len(@J);
DECLARE @TCNT	INT = 0;

DECLARE @SPOS	INT = 1;	-- curr position in @S
DECLARE @JPOS	INT = 1;	-- curr position in @J
DECLARE @FCHR	CHAR(1);	-- char to find

while @JPOS <= @JLEN
BEGIN

	SET @FCHR = SUBSTRING(@J, @JPOS, 1);

	SET @SPOS = 1;

	while @SPOS > 0 AND @SPOS <= @SLEN
	BEGIN

		SET @SPOS = charindex(@FCHR, @S COLLATE Latin1_General_CS_AS, @SPOS);

		IF @SPOS > 0 BEGIN
			SET @TCNT = @TCNT + 1;
			SET @SPOS = @SPOS + 1;
		END
	END

	SET @JPOS = @JPOS + 1;
END
print 'J='+@J+' S='+@S+' TOTAL = '+CAST(@TCNT AS VARCHAR(8));