Initcap Function

,

INITCAP returns char, with the first letter of each word in uppercase, all other letters in lowercase. Words are delimited by white space or characters that are not alphanumeric.

ALTER FUNCTION Initcap 
(
	@inString VARCHAR(MAX)
)

/*  INITCAP returns char, with the first letter of each word 
	in uppercase, all other letters in lowercase. Words are 
	delimited by white space or characters that are not 
	alphanumeric   
*/
RETURNS VARCHAR(MAX)
AS
BEGIN
	DECLARE 
		@i		INT, 
		@j		INT,
		@c		CHAR(1),
		@result VARCHAR(4000),
		@StrLength  int

	SET @StrLength = LEN(@inString)
	SET @result = LOWER(@inString)
	SET @i = 2
	SET @result = STUFF(@result,1,1,UPPER(SUBSTRING(@inString,1,1)))
	
	WHILE @i < @StrLength
	BEGIN
		SET @c = SUBSTRING(@inString,@i,1)
		IF (@c = ' ') OR 
			(@c = ';') OR 
			(@c = ':') OR 
			(@c = '!') OR 
			(@c = '?') OR 
			(@c=',' ) OR 
			(@c = '.') OR 
			(@c= '_')
		BEGIN
			SET @j = @i
			WHILE 1 = 1
			BEGIN
				if ASCII(UPPER(SUBSTRING(@inString,@j,1))) BETWEEN 65 AND 90
				BEGIN
					SET @result = STUFF(@result, @j ,1,UPPER(SUBSTRING(@inString,@j,1)))
					BREAK
				END
				SET @j = @j+ 1
			END
			SET @i = @j
		END
		SET @i = @i + 1
	END
	RETURN  @Result
END

Rate

3.33 (3)

Share

Share

Rate

3.33 (3)