I think I originally got this from Steve Jones or at least one of his posts. Made a couple of tweaks to handle Roman Numerals because we mostly use it for names.
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure sp_ProperCase
@table varchar(50),@field varchar(50)
as
-- Replace spaces with the "special" character
exec('update ' + @table + '
set ' + @field + ' = lower(replace( ' + @field + ' , '' '', ''@''))')
-- Handle case 1 - First item
exec('update ' + @table + '
set ' + @field + ' = upper( substring( ltrim( ' + @field + ' ), 1, 1)) + substring( ltrim( ' + @field + ' ), 2, 80)')
-- loop while there are rows with the flag
exec('while exists(
select *
from ' + @table + '
where ' + @field + ' like ''%@%''
)
begin
-- Proper case the word after the flag.
update ' + @table + '
set ' + @field + ' = substring( ' + @field + ' , 1, charindex( ''@'', ' + @field + ' )) +
upper( substring( ' + @field + ' , charindex( ''@'', ' + @field + ' )+1, 1 )) +
substring( ' + @field + ' , charindex( ''@'', ' + @field + ' )+2, 80)
where ' + @field + ' like ''%@%''
-- Remove the first flag encountered in each row
update ' + @table + '
set ' + @field + ' = substring( ' + @field + ' , 1, charindex( ''@'', ' + @field + ' )-1) +
'' '' + substring( ' + @field + ' , charindex( ''@'', ' + @field + ' ) + 1, 80)
where ' + @field + ' like ''%@%''
end')
exec('update ' + @table + ' set ' + @field + ' = replace(' + @field + ','' ii'','' II'') where ' + @field + ' like ''% ii''')
exec('update ' + @table + ' set ' + @field + ' = replace(' + @field + ','' iii'','' III'') where ' + @field + ' like ''% iii''')
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO