I have a small bit of code that I need to execute over and over again. I'm splitting a string into first, middle, and last names.
DECLARE @SpaceIndex int,
@LastSpaceIndex int,
@Length int
SET @Length = LEN(@Name)
SET @LastSpaceIndex = @Length - CHARINDEX(' ', REVERSE(@Name))
Set @SpaceIndex = CHARINDEX(' ', @Name)
SELECT SUBSTRING(@Name, 0, @SpaceIndex) As FirstName,
SUBSTRING(@Name, @SpaceIndex + 1, @LastSpaceIndex - @SpaceIndex) As MiddleName,
SUBSTRING(@Name, @LastSpaceIndex + 1, @Length - @LastSpaceIndex) As LastName
I've decided to turn this into a table function or sproc with output parameters, but I'm not sure which way is better.
What is the best way to return three values in SQL?
For reference names can look like this: 'John Doe', 'Doe', 'John W Doe', 'Jane Whilma Alice Doe'
Thanks