February 23, 2015 at 9:12 am
I have column called fullname which has firstname lstname example John Smith
How can I get it to Lastname, Firstname like Smith , John
February 23, 2015 at 9:14 am
February 23, 2015 at 9:23 am
Any other ideas?
February 23, 2015 at 10:21 am
This would work provided there are always 2 and only 2 names separated by a single space:
select
substring(fullname,charindex(' ',fullname) + 1,len(fullname) - charindex(' ',fullname)) + ', ' + left(fullname,charindex(' ',fullname)-1)
from YourTableName
February 23, 2015 at 10:44 am
Slightly shorter version with the same problem as the one from Adam.
WITH SampleData(fullname) AS(
SELECT 'Peter Parker' UNION ALL
SELECT 'Harry Osborn' UNION ALL
SELECT 'Mary Jane Watson' UNION ALL
SELECT 'John Jonah Jameson Jr.'
)
SELECT fullname,
STUFF( fullname, 1, charindex(' ',fullname), '') + ', ' + left(fullname,charindex(' ',fullname)-1)
FROM SampleData
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply