So I have a database of customers. I run SELECT * FROM MyTable
it gives me back several columns, one of which is the name. Looks like this:
"Doe, John"
"Jones, Bill"
"Smith, Mike"
"Johnson, Bob"
"Harry Smith"
"Black, Linda"
"White, Laura"
etc. Some are last name, first name. Others are first name last name.
My boss wants me to flip the names so they are all first then last.
So I ran this:
SELECT SUBSTRING(Column_Name, CHARINDEX(',', Column_Name) + 2, LEN(Name) - CHARINDEX(',', Column_Name) + 1) + ' ' + SUBSTRING(Column_Name, 1, CHARINDEX(',', Column_Name) - 1) FROM MyTable
The problem is that when I run that, it only runs the names until it finds one it doesn't need to flip. So in the example above, it would only give me the first four names, not all of them.
It was suggested to me that I could use the PATINDEX()
to pull out all of the names. I don't know how to use this and was hoping I could get some help with it.