bruskhickory
asked on
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,
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,
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
SELECT
p.Person1
, LEFT(p.Person1, CHARINDEX(' ', (p.Person1 + ' ') ) - 1 ) AS PersonWithComma
FROM PersonnelTable1 AS p
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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!
Thanks you very much!
However if the data is as messy as it appears to be do not expect SQL to automagically make it clean.