Giving space after comma in sql server

Learner 226 Reputation points
2021-10-26T17:27:19.1+00:00

Hello All

I am new to sql.
I need to give a space after comma in my employee name .

Please suggest how to do.

Data :
Smith,Micheal
Ben,Jeff

Output :
Smith, Micheal
Ben, Jeff

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-10-26T17:46:30.657+00:00

    Try a query:

    select replace(replace(MyColumn, ', ', ','), ',', ', ')
    from MyTable
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-10-27T01:30:48.207+00:00

    Hi @Learner

    According to your data, you could simply use replace function to replace commas with comma + space like below:

    update yourtable  
    set employeename=REPLACE(employeename,',',', ')  
    

    If there are also some other extra spaces, you could refer Viorel's answer.

    Besides, you could also try with another method like below:

    update yourtable  
    set employeename= STUFF(employeename, CHARINDEX(',', employeename)+1,0, ' ')   
    

    Best regards,
    Melissa


    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.