| Author |
Topic |
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2006-07-12 : 18:14:35
|
| declare @full_name varchar(100)SET @full_name = 'John#David#SmithHow can split these three names to a separate variable. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-07-12 : 18:28:00
|
| PARSENAME function works great for this, just as long as there aren't more than 4 parts to the string. DECLARE @full_name varchar(100)SET @full_name = 'John#David#Smith'SET @full_name = REPLACE(@full_name, '#', '.')SELECT PARSENAME(@full_name, 1) AS LastNameSELECT PARSENAME(@full_name, 2) AS MiddleNameSELECT PARSENAME(@full_name, 3) AS FirstName Here is the SQLTeam.com article that discusses this:http://www.sqlteam.com/item.asp?ItemID=15044Tara Kizeraka tduggan |
 |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2006-07-12 : 19:03:19
|
| Its works fine...Thanks a lot....Great |
 |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2006-09-11 : 13:38:36
|
| Here Iam facing another issue...@full_name = 'John#David#Smith can come as different cases : 1. 'John#David or John#David#(In this case middle_name is null) 2. 'John#David#Smith# (In this case I should have first_name,middle_name and last_name) |
 |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2006-09-11 : 14:12:28
|
| I used this but still Iam not getting the required resultsSELECT first_name = CASE WHEN PARSENAME(REPLACE(@full_name, '#', '.'), 3) IS NULL THEN PARSENAME(REPLACE(@full_name, '#', '.'), 2) ELSE PARSENAME(REPLACE(@full_name, '#', '.'), 3) END, middle_name = PARSENAME(REPLACE(@full_name, '#', '.'), 2), last_name = PARSENAME(REPLACE(@full_name, '#', '.'), 1) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-09-11 : 14:50:45
|
| parsing a string by a seperater is the easy part. It sounds like you need to determine all the rules for how to parse various names. Are the rules consistant? What are all the variation of how the following names could be coded with your seperater?Thurston Howell IIIMr. TCherMr. and Mrs. Rodney Q. DangerfieldBilly Bob Bubba BodineBe One with the OptimizerTG |
 |
|
|
drdream
Starting Member
29 Posts |
Posted - 2006-09-11 : 15:01:46
|
| Would this help?http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56499 |
 |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2006-09-11 : 16:22:01
|
| @full_name can appear in different ways'John#David#Smith''John#David#Smith''John#David#''John#David'I need to parse them into first name ,middle name and Last nameOne strange case I found was It can be'John#David#S.''John#D.#Smith''John.#David#SmithNow I can't use the PARSENAME to parse it to first name,middle name and last name |
 |
|
|
sqllearner
Aged Yak Warrior
639 Posts |
Posted - 2006-09-11 : 16:24:03
|
| @full_name can appear in different ways'John#David#Smith''John#David#Smith#''John#David#''John#David'I need to parse them into first name ,middle name and Last nameOne strange case I found was It can be'John#David#S.''John#D.#Smith''John.#David#SmithNow I can't use the PARSENAME to parse it to first name,middle name and last name |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-09-11 : 17:28:04
|
Ok that logic is getting a little more than just splitting a csv. See if this helps you out:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56499EDIT:That function does a good job at seperating the name parts however since you have some cases where Middle and Last names are reversed depending on the length you will need to modify the final results with a case statement checking for LEN of those values.perhaps something like this:use pubs--assumes the FormatName function is compiled in PUBSset nocount ondeclare @tb table (rowid int identity(1,1) ,full_name varchar(50) ,firstname varchar(15) ,middleName varchar(15) ,lastname varchar(15))insert @tb (full_name) select 'John#David#Smith' union allselect 'John#David#' union allselect 'John#David' union allselect 'John#David#S.' union allselect 'John#D.#Smith' select full_name ,F as firstName ,case when len(L) = 1 then L else M end as middleName ,case when len(L) = 1 then M else L end as LastNamefrom ( select full_name ,dbo.FormatName(replace(full_name,'#', ' '), 'F') F ,dbo.FormatName(replace(full_name,'#', ' '), 'M') M ,dbo.FormatName(replace(full_name,'#', ' '), 'L') L from @tb ) aoutput:full_name firstName MiddleName LastName----------------- -------------- ------------- ----------John#David#Smith John David SmithJohn#David# John DavidJohn#David John DavidJohn#David#S. John S DavidJohn#D.#Smith John D Smith Be One with the OptimizerTG |
 |
|
|
|