SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software SQL Server and T-SQL Development Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




download SQL Server 2017
download SQL Server 2016
download SQL Server 2014



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





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





Related SQL Resources

SQL Server Articles

SQL Server 2012

SQL Server Tools

SQL Blog

MS SQL Server Forums









Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems