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?
 
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
 
PortletPaulfreelancerCommented:
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
 
PortletPaulfreelancerCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.