I need to split a name field.
Contacts
ContactID ContactName ContactPhone ContactFax ContactCellPhone ContactEmail OfficeCode Program
5041 Smith, Jane M 866-555-1212 866-555-1313 ADM USR
7389 John.Jones@somewhere.com 866-555-2345 866-555-5678 ITS PGP
basicaly something like split [Smith, Jane M] into
lastname = Smith ; firstname = Jane M
or if
Name like *@* then split Jane.smith@somewhere.com
into ContactLastName = Jones ; ContactFirstName = John
AND
ContactEmail = John.Jones@somewhere.com
Can someone assist??
I can do this quite easily in VBS but just need a start on how to do this in SQL.
This worked Great.
Then found email addresses without periods.
Fix
Then needed a way to update the table with the split data.
suggested updating one column at a time as so.
Thanks
John Fuhrman
faq329-6766
faq329-7301
thread329-1334328
thread329-1424438
Contacts
ContactID ContactName ContactPhone ContactFax ContactCellPhone ContactEmail OfficeCode Program
5041 Smith, Jane M 866-555-1212 866-555-1313 ADM USR
7389 John.Jones@somewhere.com 866-555-2345 866-555-5678 ITS PGP
basicaly something like split [Smith, Jane M] into
lastname = Smith ; firstname = Jane M
or if
Name like *@* then split Jane.smith@somewhere.com
into ContactLastName = Jones ; ContactFirstName = John
AND
ContactEmail = John.Jones@somewhere.com
Can someone assist??
I can do this quite easily in VBS but just need a start on how to do this in SQL.
Code:
Declare @Temp Table(ContactID int, ContactName VarChar(50))
Insert Into @Temp Values(5041,'Alfred, Leslie M')
Insert Into @Temp Values(7389,'Jane.smith@somewhere.com')
Select ContactId,
ContactName,
Case When ContactName Like '%@%'
Then Left(ContactName, CharIndex('.', ContactName)-1)
When ContactName Like '%,%' Then Left(ContactName, CharIndex(',', ContactName)-1)
End As FirstName,
Case When ContactName Like '%@%'
Then SubString(ContactName, CharIndex('.', ContactName)+1, CharIndex('@', ContactName)-CharIndex('.', ContactName)-1)
When ContactName Like '%,%'
Then Right(ContactName,Len(ContactName)-CharIndex(',', ContactName)-1)
End As LastName,
Case When ContactName Like '%@%' Then ContactName End As EmailAddress
From @Temp
This worked Great.
Then found email addresses without periods.
Fix
Code:
Select
ContactName,
Case When ContactName Like '%@%' and ContactName Like '%.%'
Then Left(ContactName, CharIndex('.', ContactName)-1)
When ContactName Like '%,%' Then Left(ContactName, CharIndex(',', ContactName)-1)
End As ContactFirstName,
Case When CharIndex('@', ContactName) > CharIndex('.', ContactName)
Then SubString(ContactName, CharIndex('.', ContactName)+1, CharIndex('@', ContactName)-CharIndex('.', ContactName)-1)
When ContactName Like '%,%'
Then Right(ContactName,Len(ContactName)-CharIndex(',', ContactName)-1)
End As ContactLastName,
Case When ContactName Like '%@%' Then ContactName End As ContactEmail
from dbo.Contacts
Then needed a way to update the table with the split data.
suggested updating one column at a time as so.
Code:
-- Extract eMaiil Addresses from ContactName
Update dbo.Contacts
Set ContactEmail = Case When ContactName Like '%@%' Then ContactName End
Where ContactEmail is NULL or ContactEmail = '
Thanks
John Fuhrman
faq329-6766
faq329-7301
thread329-1334328
thread329-1424438