SQL Server administration and T-SQL development, Web Programming with ASP.NET and Javascript, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz SQL Server and T-SQL Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Vista, etc.






SQL Capitalize First Letter - SQL Capitalize String

Here is a t-sql script which can be used to capitalize string in SQL Server development.
The below sql codes will capitalize first letter in a word.
The input is a sql string which is converted to upper case letters at first letter of each word.
Words in a sentence are distinguished by SPACE character.

DECLARE @Word nvarchar(max)
set @Word = 't-sql tutorial - sql capitalize first letter - sql capitalize string'
DECLARE @Word2 nvarchar(max) = @Word

select @Word = STUFF(LOWER(@Word),1,1,upper(left(@word,1)))

declare @i int, @j int
set @i = CHARINDEX(' ',@Word2,1)

while @i > 0
begin
  select @j = @i, @i = @i + 1
  select @Word = STUFF(@word,@i,1,upper(SUBSTRING(@word,@i,1)))
  select @Word2 = STUFF(@word2,@j,1,'-')
  select @i = CHARINDEX(' ',@Word2,1)
end

select @Word

The output of the t-sql capitalize first letter script is as follows :
T-sql Tutorial - Sql Capitalize First Letter - Sql Capitalize String




Free SQL Comparison tools
Trusted by thousands of users
Download your copy now



An other method is to split the sentence into words, capitalize first letter then sql concatenate string back

SELECT
 STUFF(
  (
  SELECT
   ' ' + STUFF(val,1,1,upper(left(val,1)))
  FROM dbo.split(@word,' ')
  FOR XML PATH('')
  ), 1, 1, ''
 ) As capitalize_string





Follow Kodyaz on Twitter

Related SQL Resources

SQL Server Articles

SQL Server 2012

SQL Server Tools

SQL Blog

SQL Server 2008 Blog

Certification Exams Blog

Reporting Services Blog

Analysis Services Blog

MS SQL Server Forums



Free Exam Vouchers









Copyright © 2004 - 2012 Eralper Yilmaz. All rights reserved.
Community Server, by Telligent Systems