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

Medium Priority
1,158 Views
Last Modified: 2012-05-06
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

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:

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

Ask the Experts
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

SharathData Engineer
CERTIFIED EXPERT
Commented:

Author

Commented:
Thanks.


I was hoping for something a lot simpler.  Will give them a look when i get a chance.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

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

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.