make first letter capital of the word

Shambhu Rai 1,411 Reputation points
2022-03-24T15:34:05.797+00:00

Hi Expert,

how to make first letter capital of the word

create table main13( First_lastname Nvarchar (100))

insert main13 values('18 - EW MEMBER; OLD MEEMBER'),
('18 - VOYGER'),
('1 - ONE DAY')

EXPECTED Output

186478-image.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,004 questions
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,592 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Guoxiong 8,206 Reputation points
    2022-03-24T16:35:30.003+00:00

    I think you need a function to archive it. Here is the function which returns the string with first letter capital of each word:

    CREATE FUNCTION [dbo].[ufn_Get_String_with_First_Letter_Capital_of_Each_Word] (  
    	@inputString varchar(100),  
    	@delimiter char(1)  
    )  
    RETURNS VARCHAR(100)  
    AS    
    BEGIN   
    	DECLARE @outputString varchar(100) = '';  
    	DECLARE @word varchar(100) = '';  
    	DECLARE @pos int = 0;  
    	DECLARE @len int = 0;  
      
    	SET @inputString = CASE WHEN RIGHT(@inputString, 1) <> @delimiter THEN @inputString + @delimiter ELSE @inputString END;  
    	WHILE CHARINDEX(@delimiter, @inputString, @pos + 1) > 0  
    	BEGIN  
    		SET @len = CHARINDEX(@delimiter, @inputString, @pos + 1) - @pos;  
    		SET @word = SUBSTRING(@inputString, @pos, @len);  
    		SET @word = UPPER(LEFT(@word, 1)) + LOWER(SUBSTRING(@word, 2, LEN(@word) - 1))  
      
    		IF @pos = 0  
    		BEGIN  
    			SET @outputString = @word;  
    		END  
    		ELSE  
    		BEGIN  
    			SET @outputString = @outputString + ' ' + @word;  
    		END  
      
    		SET @pos = CHARINDEX(@delimiter, @inputString, @pos + @len) + 1  
    	END  
    	  
    	RETURN @outputString;  
    END  
    GO  
    

    And then use the following query to the result:

    SELECT [dbo].[ufn_Get_String_with_First_Letter_Capital_of_Each_Word](First_lastname, ' ')  
    FROM main13;  
    

    186449-image.png

    0 comments No comments

  2. Naomi Nosonovsky 7,886 Reputation points
    2022-03-24T16:39:50.153+00:00

    John Ross answer is what I was going to suggest as well, but read the comments please as for the names it may be a very tricky problem

    https://stackoverflow.com/questions/230138/sql-server-make-all-upper-case-to-proper-case-title-case

    0 comments No comments

  3. Shambhu Rai 1,411 Reputation points
    2022-03-24T19:14:50.297+00:00

    what a solution ..is there any easiest way to do this just want to have capital word after space


  4. DJAdan 671 Reputation points
    2022-03-24T19:52:59.907+00:00

    Hi ShambhuRai

    This solution works for me:

    declare @foo table
    (
     id integer identity(1,1),
     first_lastname varchar(max)
    );
    
    insert into @foo values
    ('18 - EW MEMBER; OLD MEEMBER'),
    ('18 - VOYGER'),
    ('1 - ONE DAY');
    
    with cte as
    (
    select
     f.id,
     new_value = upper(left(s.value, 1)) + SUBSTRING(s.value, 2, len(s.value)),
     word_id   = row_number() over (partition by f.id order by (select null))
    from
     @foo f
     cross apply string_split(lower(f.first_lastname), ' ') s
    )
    select
     c.id,
     First_lastname = string_agg(c.new_value, ' ') within group (order by c.word_id)
    from
     cte c
    group by
     c.id;
    
    0 comments No comments

  5. Bert Zhou-msft 3,431 Reputation points
    2022-03-25T02:14:13.437+00:00

    Hi,@Shambhu Rai

    Welcome to Microsoft T-SQL Q&A Forum!

    Please check this:

    CREATE FUNCTION Capital_Word(@Text NVARCHAR(200))  
    RETURNS NVARCHAR(200)  
    AS  
    BEGIN  
        RETURN STUFF((  
            SELECT ' ' + UPPER(LEFT(s.value,1)) + LOWER(SUBSTRING(s.value,2,LEN(s.value)))  
            FROM   
     OPENJSON('["' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@Text,'\','\\'),'"','\"'),CHAR(9),'\t'),CHAR(10),'\n'),' ','","') + '"]') s  
            ORDER BY s.[key]  
        FOR XML PATH(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)'),1,1,'');  
    END  
     
     SELECT First_lastname,dbo.Capital_Word(First_lastname)as Updated_First_lastname  
     FROM #main13;  
    

    Here is the result:
    186774-image.png

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.