Skip to content
Advertisement

SQL Server – Getting initials from a list of names

I have a table containing a list of employees assigned to a certain project. A bit like this :

I have to transform the Employees column to get the initial of the first name and get the full last name. The result should look like this :

Problem occurs when the value contains more then one name. What can be done for this?

Advertisement

Answer

Solution for PostgreSQL. Assuming that the multiple names in a cell are consistently separated by a comma and a following space you could


1 expand all multi name cells into separate rows
2 split the column with the names into separate columns representing first name and last name
3 extract first letter from the first name and join it with the last name



if you want to have the multiple names in the cells back, you can aggraegate them in the final step

but then again it is all depending on clean consistent data in your table

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement