Create FUNCTION [dbo].[fnInitialCaps](@InputString AS VARCHAR(8000))
RETURNS VARCHAR(8000)
AS
BEGIN  
 DECLARE @Reset BIT
 DECLARE @ResultString VARCHAR(8000)
 DECLARE @index INT
 DECLARE @CurentChar CHAR(1)
 SELECT @Reset = 1, @index=1, @ResultString = ''
 --Find and replace the Initial Caps
 WHILE (@index <= LEN(@InputString))
 SELECT @CurentChar= SUBSTRING(@InputString,@index,1),
 --Reset Yes means previous char is not an alphabet
 @ResultString = @ResultString +
     CASE WHEN @Reset = 1 THEN UPPER(@CurentChar) ELSE LOWER(@CurentChar) END,
    -- Check the next character is a Alphabet, if Yes 0
 @Reset = CASE WHEN @CurentChar like '[a-zA-Z]' THEN 0 ELSE 1 END,          
 @index = @index + 1  

RETURN @ResultString
END

Comments (0)