Your technology certification is waiting. Enroll in Cloud Class ®
drop function dbo.titlecase
create function dbo.titlecase(@input nvarchar(max))
set @input = lower(@input)
declare @values table(position int not null primary key clustered, theNCHar nchar(1))
;with cte0 as (select 1 as c union all select 1),
cte1 as (select 1 as c from cte0 a, cte0 b),
cte2 as (select 1 as c from cte1 a, cte1 b),
cte3 as (select 1 as c from cte2 a, cte2 b),
cte4 as (select 1 as c from cte3 a, cte3 b),
cte5 as (select 1 as c from cte4 a, cte4 b),
nums as (select row_number() over (order by c) as n from cte5)
insert into @Values (position,theNChar)
select n, substring(@input, n,1)
where n <= datalength(@input)/2
order by n
set theNchar = upper(v.theNChar)
from @values v
left join @values v2
on V.position = v2.position+1
where v2.theNChar=N' '
or v2.theNChar is null
select @input = isnull(@input,N'') + theNChar
order by position
select dbo.titlecase(N'THE CAT SAT ON THE MAT')
Open in new window
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.