We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# TSQL: Replace column with Title Case

on
Medium Priority
1,158 Views
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

Comment
Watch Question

## View Solutions Only

Chief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

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')
``````
Data Engineer
CERTIFIED EXPERT
Commented:

Commented:
Thanks.

I was hoping for something a lot simpler.  Will give them a look when i get a chance.
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile