Author |
Topic |
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2010-08-13 : 17:53:58
|
hii have a colomn called name the data in it is stored in the following mannerlastname,firsname i.e doe,johnsmith,johni wnat split the field into two separate fileds such as lastname and firstname.How can i achieve this..thanks in advance |
|
Humate
Posting Yak Master
101 Posts |
Posted - 2010-08-13 : 18:20:18
|
Is it always seperated by comma? Maybe you can work with this:DECLARE @Names TABLE ( Name VARCHAR (50), Surname VARCHAR (50), Firstname VARCHAR (50) )INSERT @NamesSELECT 'doe,john', NULL, NULL UNION ALLSELECT 'cratchet,bob', NULL , NULL UNION ALLSELECT 'jordan,michael', NULL, NULLUPDATE @NamesSET Surname = LEFT(LTRIM(RTRIM(name)),CHARINDEX(',',LTRIM(RTRIM(name)))-1),Firstname = RIGHT(LTRIM(RTRIM(name)),LEN(LTRIM(RTRIM(name)))-CHARINDEX(',',LTRIM(RTRIM(name))))SELECT * FROM @Names |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 01:05:10
|
assuming format is consistent ie. there will be two parts of name separated by , you can useUPDATE @NamesSET Surname = PARSENAME(REPLACE(name,',','.'),1),Firstname = PARSENAME(REPLACE(name,',','.'),2) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2010-08-16 : 11:22:21
|
thanks visakh and humate . Actuallythere are some names with the middle intial in the following way:doe,john msmith,james FHow can split the middle intial now...thanks in advance |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 11:25:30
|
[code]UPDATE @NamesSET Surname = PARSENAME(REPLACE(REPLACE(name,',','.'),' ','.'),3),MiddleInitial = PARSENAME(REPLACE(REPLACE(name,',','.'),' ','.'),1),Firstname = PARSENAME(REPLACE(name,',','.'),2)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2010-08-16 : 13:48:57
|
thank you visakh it works butwhen there is no middle intial for a name then it returns null valuelike smith,johnits returns the value only for smith,john RANd there is one more problem...there is trailing space in front of the first name likesmith,(space) john Rthanks in advance |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 13:53:07
|
what other formats can name come?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2010-08-16 : 14:16:35
|
thats the only formatex:doe,johnsmith, james Rann, mary Kso..on this is the standard format with which the field is populated |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 14:29:19
|
[code]UPDATE @NamesSET Surname = LEFT(name,CHARINDEX(',',name)-1),MiddleInitial = CASE WHEN CHARINDEX(' ',name,CHARINDEX(',',name))>0 THEN SUBSTRING(name,CHARINDEX(' ',name,CHARINDEX(',',name))+1,1) ELSE NULL END,Firstname = LTRIM(SUBSTRING(name,CHARINDEX(',',name)+1,CASE WHEN CHARINDEX(' ',name,CHARINDEX(',',name))>0 THEN LEN(name)-2 ELSE LEN(name) END))[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2010-08-16 : 15:05:26
|
thanks visakh for your constant help .....actually after much research i found out there is another field customer name where the names are in the following formatjohn DoeJohn R smithJames A marySo i used this query belowDECLARE @FullName VARCHAR(100)SET @FullName = 'John Doe'SELECT SUBSTRING(@FullName, 1, CHARINDEX(' ', @FullName) - 1) AS [FirstName], SUBSTRING(@FullName, CHARINDEX(' ', @FullName) + 1, LEN(@FullName)) AS [LastName]the output i got is like this:firstname Lastnamejohn DOEJohn R Smithetccan you help me with separating the middle intial from the lastname |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-17 : 10:57:50
|
[code]UPDATE @NamesSET Surname = PARSENAME(REPLACE(REPLACE(name,',','.'),' ','.'),1),MiddleInitial = PARSENAME(REPLACE(REPLACE(name,',','.'),' ','.'),2),Firstname = PARSENAME(REPLACE(name,',','.'),1)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2010-08-17 : 16:37:31
|
thanks visakh 16 ..it worked btw you blog is very nice..hope you keep it updated regurlarly. |
 |
|
|
|
|