Make capital letter the first character of each word in a sentence in Sqlserevr.

Input: ‘hi, this is Sunil Kumar Meena. I am testing my query using This string.’

Output: ‘Hi, This Is Sunil Kumar Meena. I Am Testing My Query Using This String.’

DECLARE @str nvarchar(1000)
SET @str = ‘hi, this is Sunil Kumar Meena. I am testing my query using This string.’

DECLARE @length int,@pos int
SET @pos=0
SET @length = LEN(@str) – LEN(replace(@str,’ ‘,”))+1

WHILE(@length > 0)
    begin
        SELECT @pos = CHARINDEX(‘ ‘,@str,@pos)+1, @str =STUFF(@str,@pos,1,char( case when ASCII(SUBSTRING(@str,@pos,1)) >= 97 then ASCII(SUBSTRING(@str,@pos,1)) – 32 else ASCII(SUBSTRING(@str,@pos,1)) end))
        set @length = @length – 1
    end
SELECT @str

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s