I have a table on my database. My table\'s name is \"Company\". I want to change data \"company_name\" as upper case first letter. For example;
\"ABC COMPANY\"
With indebtedness to the above post, this function capitalizes the first letter of every word except those that are less than a certain character length, which are assumed to be acronyms. If that's no issue, then you set the second argument to 0.
CREATE function [dbo].[f_camel_exc_short_words] (@InputString varchar(4000),@AcronymMaxLen INT )
RETURNS VARCHAR(4000)
AS
BEGIN
DECLARE @Index INT
DECLARE @Char CHAR(1)
DECLARE @PrevChar CHAR(1)
DECLARE @Word VARCHAR(255)
DECLARE @WordChar CHAR(1)
DECLARE @OutputString VARCHAR(255)
DECLARE @WordIndex INT
SET @Word = ''
SET @OutputString = ''
SET @Index = 1
WHILE @Index <= LEN(@InputString)+1
BEGIN
SET @Char = SUBSTRING(@InputString, @Index, 1)
SET @PrevChar = CASE WHEN @Index = 1 THEN ' ' ELSE SUBSTRING(@InputString, @Index - 1, 1) END
--IF @Char IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')
-- SET @OutputString = @OutputString + @Char
IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(','0','1','2','3','4','5','6','7','8','9') or @Index = LEN(@InputString)+1
BEGIN
SET @WordIndex = 1
IF LEN(@Word) > @AcronymMaxLen
BEGIN
WHILE @WordIndex <= LEN(@Word)
BEGIN
SET @WordChar = SUBSTRING(@Word,@WordIndex,1)
if @WordIndex = 1 begin
SET @Word = STUFF(@Word,@WordIndex,1,UPPER(@WordChar)) end
else begin
SET @Word = STUFF(@Word,@WordIndex,1,LOWER(@WordChar)) end
SET @WordIndex = @WordIndex + 1
END
END
ELSE BEGIN
SET @Word = UPPER(@Word)
END
set @OutputString = @OutputString + @Word
SET @Word = ''
END
SET @Word = @Word + @Char
SET @Index = @Index + 1
END
return @OutputString
end
GO
So PRINT dbo.f_camel_exc_short_words ('PIONEER EURO BOND FUND CLASS C (NON-DIST) (EUR) (OFFSHORE) ISIN LU0119429891',4)
returns
Pioneer EURO BOND FUND Class C (NON-Dist) (EUR) (Offshore) ISIN LU0119429891
Hope it helps.