Q: SQL Server: capitalize first letter of each word without using function Any suggestions on to how to capitalize only the first letter of each word in a cell with select statement - without using a function? I do not have permission to create functions/views/tables in the database, so I need to run a query/select statement automated in a batchjob with OGR.
I can only find examples where the first letter of the whole cell is capitalized OR solving the issue with a function.
It concerns a name-column with strings like:
'JOHN DOE'
'JOHN JANE-DOE'
'JOHN-JANE DOE'
''
'JOHN'

A: Just for fun
Declare @YourTable table (ID int,Name varchar(50))
Insert Into @YourTable values
(1,'JOHN DOE'),
(2,'JOHN JANE-DOE'),
(3,'JOHN-JANE DOE'),
(4,''),
(5,'JOHN'),
(6,'PATTY O''BRIAN'),
(7,'OLD MCDONALD')


Declare @Str varchar(max) = (Select ID,Name=Lower(Name) from @YourTable for XML Raw)
Select  @Str = Replace(@Str,MapFrom,MapTo)
From  (
        Select MapFrom=P+C
              ,MapTo  =Upper(P+C)
         From (Select * From (Values ('"'),(' '),('-'),(' O'''),(' Mc')) P (P)) A
         Cross Join (Select Top 26 C=Char(96+Row_Number() Over (Order By Number)) From master..spt_values) C
         Union All 
         Select * From (Values (' MC',' Mc')) P (MapFrom,MapTo)
    ) A
Declare @XML xml = cast(@Str as XML)
Select ID   = r.value('@ID','int')
      ,Name = r.value('@Name','varchar(50)')
 From  @XML.nodes('/row') as A(r)
 Cross Apply A.r.nodes('./@*') AS B(attr)
 Where attr.value('local-name(.)','varchar(100)') not in ('ID','Active') 

Returns
ID  Name
1   John Doe
2   John Jane-Doe
3   John-Jane Doe
4   
5   John
6   Patty O'Brian
7   Old McDonald


A: Sorry, I'm new to Stackoverflow and I was not aware of the degree of complexity of this question. 
I found a workaround for now - the company that provides our database infratructure provides an additional table that has the correct formatted names. So I've joined the correct formatted names on the table that I'm working with.
Thank you for the help.
