Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to separete this data
KIM, SUSAN H
but it is not seperate in form of lastname, firstname & middle

What I have tried:

i have use charindex() & substring() in sql
Posted
Updated 1-Aug-18 8:07am
Comments
ZurdoDev 1-Aug-18 13:12pm    
Yes, you need to use CHARINDEX or STRING_SPLIT() or some other text function.
Member 11776570 2-Aug-18 5:31am    
But how i have used it but one name has onlu lastname & firstname.
ZurdoDev 2-Aug-18 8:03am    
Then possibly an IF statement.

You have to look at all the combinations of the data and then start writing rules out in English and then translate that into SQL.
Eric Lynch 1-Aug-18 13:28pm    
Be careful though, any technique will be culture-dependent. Also, even in cultures that have a clear first, middle, last, you need to consider the odd ball cases. People with no middle name and people with multiple middle names. Also, there are the occasional truly exceptional single-name cases (e.g. Cher).

1 solution

That is a presentation layer problem, and as has been mentioned is complicated by cultural issues as well as exceptions. For example, one gentleman in the UK changed his surname to "The Yorkshire Bank pLC are fascist b*st*ards" - and that is his legal name used on all official documents. There isn't a system in the world which can differentiate that from "Anna Bertha Cecilia Diana Emily Fanny Gertrude Hypatia Inez Jane Kate Louise Maud Nora Ophelia Prudence Quince Rebecca Sarah Teresa Ulysis Venus Winifred Xenophon Yetty Zeno Pepper" who was born in 1883. Then there are people like me who have no middle names at all.

The only sensible way to do this is to get the information from the user in separate text boxes, and store them in separate columns: post processing this will be horribly complex in SQL (which has very basic string manipulation features at best) and will fail in a huge number of cases.
 
Share this answer
 
v2
Comments
Eric Lynch 1-Aug-18 20:20pm    
Yeah, I went to school with a kid who's entire legal name was G...and this was way before the other OG was a recognized title :)
Member 11776570 2-Aug-18 5:13am    
Here i have not asked for the school name or to make fum of it. I just asked for the answer.
OriginalGriff 2-Aug-18 5:30am    
And I've explained that there is not simple answer.
There's a complicated answer that won't work a lot of the time, and there's "redesign and do it properly". But there is no "one line of code that does magic".
Sorry - but that's the truth.
Member 11776570 2-Aug-18 5:43am    
i have used this
select distinct
SUBSTRING([columnname], CHARINDEX(' ', [columnname]) +1,LEN([columnname])- CHARINDEX(' ',reverse([columnname]))) AS firstname

,substring([columnname],isnull(nullif(charindex(' ',[columnname],charindex(' ',[columnname])+(1)),(0)), charindex(' ',[columnname])),
case
when
charindex(' ', [columnname])=(0)
then
(0)
else
len([columnname])
end) as minit
,[columnname] as orignal_data
from [tablename]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900