How to remove a "space" from a name filed and move the next text characters over to another column.

Posted on 2008-11-12
Last Modified: 2012-05-05
We have about 12K records where name is " JOHN" or "JOHN  J" or "JOHN ", I have heard there is a simple way to find and replace the space character, however I need to move any character after the space to another Colum.


Needs to be
---------   ----------  

JOHN      J
Question by:sqlagent007
    1 Comment
    LVL 39

    Accepted Solution

    try this
    select case when charindex(' ',fname)>0 then left(fname,charindex(' ',fname)-1)
    else fname
    end as fname
    , case when charindex(' ',fname)>0 then right(fname, len(fname)-charindex(' ',fname))
    else NULL
    end as mInit
    --from here down is just sample data
    (select 'JOHN J' as fName
    union all
    select 'john')Names

    Open in new window


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Add another "Having" clause? 9 39
    SQL Server DatePart HOUR 6 27
    Stored Procedure error 45 36
    Stored procedure 4 21
    There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
    Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now