I have a table that stores Firstname and Middle name in Field 1 and Lastname and Suffix name in Field 2.
I have to seperate the first and middle name and put in 2 fields. But the problem is I cannot just check for the space inbetween. since some first names have spaces in between. And then another space for Middle name. Same with the lastname and suffix.
How would I seperate these fields into different columns.

Sql Question
mware
Amit
jkotas
ehab ghabour
Yea, the other question is how much data do you have If you have a hundred rows, just use a simple substring and then scan for invalid data. If you have millions, then a decent algorithm like this should be applied, but you might just need to create a table of rows that have been reviewed and force the next user who touches the row to look at the data for correctness.
Like r sτσρhΞr said, these are really business rule questions, and name data is really tricky since users will try really hard to put the data into the space allotted so I wouldn't be surprised if data like [Joeseph Joey] [San Sebastian III] might be found.
Good luck :)
Terry125
You can do most anything I am not really clear what you want exactly, but you can use charindex, ltrim, substring, and len to build a check:
declare @name varchar(30)
select @name = 'Fred Smith'
select substring(@name,1,charindex(' ',@name))
select case when len(ltrim(substring(@name,charindex(' ',@name),len(@name)))) <= 3 then
ltrim(substring(@name,charindex(' ',@name),len(@name)))
else '' end as ShortName,
case when len(ltrim(substring(@name,charindex(' ',@name),len(@name)))) > 3 then
ltrim(substring(@name,charindex(' ',@name),len(@name)))
else '' end as LongName
And yes, it's ugly as homemade sin, but you can piece together the different length parts and checks to split a string into parts. It is just messy (which you can gather) and why it is always suggested not to do it this way (which you are apparently undoing right now :)
octagon
This is really a business rule question, rather than a technical question. You first need to determine the basis on which you will split the data up. The surname case seems easiest, as a surname usually doesn't include a space. You need to come up with an acceptable rule (in conjunction with the business owners of the data) on the basis by which you split up the data. For example:
The same applies to the first name, but this is more tricky. For example, in the case of "Mary Anne Jane", with no hyphen, you need to decide whether "Anne" is part of the first name, or the middle name. It would be easiest just to come up with a simple rule such as
... I would suggest proposing something logical based on your review of the data, then proposing it to the data stakeholders for the confirmation, before implementation.