I’m look for the simplest way to split up first and last name wile trimming out the middle initial. The current layout of the field is [Last Name], [First Name] [MI]. Also, middle initial is not always there. My current code is below, I’m just not sure how to trim out the middle initial from first name without writing a case statement.
SELECT SUBSTRING(h.Name, CHARINDEX(',', h.Name, 0) + 2, LEN(h.Name) - CHARINDEX(',', h.Name, 0)), 0 as FirstName
,SUBSTRING(h.Name, 0, CHARINDEX(',', h.Name, 0)) as LastName
FROM Members
I have made some assumptions below:
1 – First names are always longer than one character.
2 – Middle inital will always be preceded by a space.
3 – The data is trimmed.
This code will return
NULLif any of the above are not true. If your data is not trimmed, you can useRTRIMon all instances of@nbelow to mitigate.