TSQL: Replace column with Title Case

Hi All,

I have a column of data that i want to replace with the same data but as Title Case.

e.g

THE CAT SAT ON THE MAT
~ is replaced with
The Cat Sat On The Mat


LVL 2
detox1978Asked:
Who is Participating?
 
BrandonGalderisiConnect With a Mentor Commented:
Here you go!
drop function dbo.titlecase
go
create function dbo.titlecase(@input nvarchar(max))
returns nvarchar(max)
as
begin
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)
from    Nums
where   n <= datalength(@input)/2 
order by n
 
update V
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
 
set @input=null
select @input = isnull(@input,N'') + theNChar 
from @Values
order by position
 
return @input
end
go
 
select dbo.titlecase(N'THE CAT SAT ON THE MAT')

Open in new window

0
 
detox1978Author Commented:
Thanks.


I was hoping for something a lot simpler.  Will give them a look when i get a chance.
0
Question has a verified solution.

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.

All Courses

From novice to tech pro — start learning today.