create function dbo.pTitleCase
(
@StrIn nvarchar(max)
)
returns nvarchar(max)
as
begin;
declare @StrOut nvarchar(max),
@CurrentPosition int,
@NextSpace int,
@CurrentWord nvarchar(max),
@StrLen int,
@LastWord bit;
set @NextSpace=1;
set @CurrentPosition=1;
set @StrOut='';
set @StrLen=len(@StrIn);
set @LastWord=0;
while @LastWord=0
begin;
set @NextSpace=charindex(' ',@StrIn,@CurrentPosition+1);
if @NextSpace=0
begin;
set @LastWord=1;
set @NextSpace=@StrLen;
end;
set @CurrentWord=upper(substring(@StrIn,@CurrentPosition,1));
set @CurrentWord=@CurrentWord+lower(substring(@StrIn,@CurrentPosition+1,@NextSpace-@CurrentPosition));
set @StrOut=@StrOut+@CurrentWord;
set @CurrentPosition=@NextSpace+1;
end;
return @StrOut;
end;