Himanshu Namdeo
Himanshu Namdeo

Reputation: 27

Remove white spaces from string and convert into title case

Here is the example which i want in output...

I have this input = "Automatic email sent"

But I want this output = "AutomaticEmailSent"

Thanks In Advance!

Upvotes: 0

Views: 1675

Answers (3)

Alex K.
Alex K.

Reputation: 175816

SQL Server

declare @value varchar(64) =  rtrim(' ' +  'Automatic email sent')
;with t(n) as (
    select n = charindex(' ', @value, 0)
    union all 
    select n = charindex(' ', @value, n + 1)
    from t
    where n > 0
)
select @value = stuff(@value, n + 1, 1, upper(substring(@value, n + 1, 1))) from t where n > 0
select replace(@value, ' ', '')

Upvotes: 0

DavidG
DavidG

Reputation: 118987

Stealing a function from this answer which takes an text input and make it proper case (otherwise known as title case):

create function ProperCase(@Text as varchar(8000))
returns varchar(8000)
as
begin
   declare @Reset bit;
   declare @Ret varchar(8000);
   declare @i int;
   declare @c char(1);

   select @Reset = 1, @i=1, @Ret = '';

   while (@i <= len(@Text))
    select @c= substring(@Text,@i,1),
               @Ret = @Ret + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
               @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end,
               @i = @i +1
   return @Ret
end

Then you can combine this function with REPLACE:

SELECT REPLACE(dbo.ProperCase(column), ' ', '')
FROM MyTable

Upvotes: 1

Waqas Raja
Waqas Raja

Reputation: 10870

Use TextInfo.ToTitleCase

// Defines the string with mixed casing. 
string myString = "Automatic email sent";

// Creates a TextInfo based on the "en-US" culture.
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;

// Changes a string to titlecase, then replace the spaces with empty
string outputString = myTI.ToTitleCase(myString).Replace(" ", "");

Upvotes: 1

Related Questions