I want to add a comma in between first name and surname in an sql query.

Hi Experts,

I am creating a query to retrieve information for a report. However I need to break up a personnel column to have the format 'First Name, Surname'.
I can get the first name ok but I am having mixed results trying to get the surname especially if the name is just initials.
SELECT      
p.Person1,
LEFT(p.Person1, CASE WHEN charindex(' ', p.Person1) = 0 THEN LEN(p.Person1) ELSE charindex(' ', p.Person1) - 1 END) as PersonWithComma
FROM      PersonnelTable1 AS p
            
The column p.Person1 has nulls and the surnames are sometimes just initials. E.g. 'Peter G F'
Actually its the surnames where it has more than one space that is causing me the biggest headaches.

I'm not a programmer so not sure what is the best way to do this. E.g. I believe functions can cause a bit more of a performance hit?

Many thanks,
bruskhickoryAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
Please provide SAMPLE DATA.
However if the data is as messy as it appears to be do not expect SQL to automagically make it clean.
0
PortletPaulEE Topic AdvisorCommented:
by the way, ADDING the character you search for makes this a bit simpler.

SELECT
    p.Person1
  , LEFT(p.Person1, CHARINDEX(' ', (p.Person1 + ' ') ) - 1 ) AS PersonWithComma
FROM PersonnelTable1 AS p
0
Kusala WijayasenaSoftware EngineerCommented:
Try This:

SELECT
p.Person1,
STUFF(p.Person1, CHARINDEX(' ', p.Person1), 1, ', ') as PersonWithComma
FROM PersonnelTable1 AS p
WHERE p.Person1 IS NOT NULL

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
I can't tell for sure if your data has the First name or the Surname at the start.  Either way, I'd use some type of computed column to set the First Name and Surname (Last Name); I'm using direct CROSS APPLYs below, but an in-line-table-valued CROSS APPLY function would probably be best, once we know the full requirements.

Then just combine the already split name parts in the final SELECT.  That way the logic of how to determine FN & LN can change without affecting the final SELECT list.


SELECT Person1,
    Person1_First_Name + ISNULL(', ' + Person1_Surname, '') AS Person1_Final_Name
FROM (
    SELECT 'Peter G F' AS Person1 UNION ALL
    SELECT 'Jane Doe' UNION ALL
    SELECT 'H G Wells' UNION ALL
    SELECT 'Billy Joel' UNION ALL
    SELECT 'Giselle'
) AS test_data
CROSS APPLY (
    SELECT CHARINDEX(' ', Person1 + ' ') AS Person1_First_Space,
        CHARINDEX(' ', Person1 + ' ', CHARINDEX(' ', Person1 + ' ') + 1) AS Person1_Second_Space
) AS name_find_spaces
CROSS APPLY (
    SELECT LEFT(Person1, Person1_First_Space - 1) AS Person1_First_Name,
        CASE WHEN Person1_Second_Space = 0 THEN NULL
            ELSE SUBSTRING(Person1, Person1_First_Space + 1, Person1_Second_Space - Person1_First_Space - 1) END AS Person1_Surname
0
bruskhickoryAuthor Commented:
Hi Kusala, this worked perfect. I have not seen 'Stuff' used before. But it allows for Nulls etc. I have done a select on the whole dataset and all converts correctly.
Thanks you very much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.