David Glover
asked on
How to pick out an email address in amongst a varchar(255) string using SQL Server 2000
I have a field in a table which is of type varchar(255) and may contain an email address in amongst other text. The email address will have a space before and after it. I wish to select/display the email address. There may be more than one email address, I would prefer to display only the first occurence in this circumstance.
Please give this a try:
select right(
left(yourfield, charindex('@', yourfield) - 1),
charindex(' ', reverse(left(yourfield, charindex('@', yourfield) - 1)))
) +
substring(yourfield, charindex('@', yourfield) + 1, charindex(' ', yourfield, charindex('@', yourfield) + 1))
refined one:
select right(
left(yourfield, charindex('@', yourfield) - 1),
charindex(' ', reverse(left(yourfield, charindex('@', yourfield) - 1)))
) +
substring(yourfield, charindex('@', yourfield) , charindex(' ', yourfield, charindex('@', yourfield)))
As Gallatin said please show some examples and teh o/p u expected?
ASKER
This is an example...
I Parkinson - E-Mail Subject: FW: CV Basket CV: ian parkinson
From: victoria.stout@first-caree rs.co.uk (Victoria Stout)
Cheers my dears!
-----Original Message-----
From: Jobsite CV Basket [mailto:null@jobsite.co.uk ]
Sent: 11 May 2004 15:13
To: v.....
I'de want the first email : victoria.stout@first-caree rs.co.uk to be returned
I'll give the suggestions a try :)
I Parkinson - E-Mail Subject: FW: CV Basket CV: ian parkinson
From: victoria.stout@first-caree
Cheers my dears!
-----Original Message-----
From: Jobsite CV Basket [mailto:null@jobsite.co.uk
Sent: 11 May 2004 15:13
To: v.....
I'de want the first email : victoria.stout@first-caree
I'll give the suggestions a try :)
Have you tried mine?
ASKER
Hi ralmada,
I think your suggestion will work but can you enhance it so it can cope with an empty/null 'yourstring', returning nothing instead.
Regards,
I think your suggestion will work but can you enhance it so it can cope with an empty/null 'yourstring', returning nothing instead.
Regards,
Sure. Here you go
select CASE WHEN charindex('@', yourfield) <> 0 then
right(
left(yourfield, charindex('@', yourfield) - 1),
charindex(' ', reverse(left(yourfield, charindex('@', yourfield) - 1)))
) +
substring(yourfield, charindex('@', yourfield) , charindex(' ', yourfield, charindex('@', yourfield)))
ELSE ' ' END as youremail
from yourtable
ASKER
Ralmada, that's nearly it, it returns the email address dropping the stuff in front but also returns everything after it also :)
try this
select CASE WHEN charindex('@', yourfield) <> 0 then
right(
left(yourfield, charindex('@', yourfield) - 1),
charindex(' ', reverse(left(yourfield, charindex('@', yourfield) - 1)))
) +
substring(yourfield,
charindex('@', yourfield) ,
charindex(' ', yourfield, charindex('@', yourfield) - charindex('@', yourfield))
)
ELSE ' ' END as youremail
from yourtable
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for this, worked great and taught me a chunk more about SQL string handling!
Something like?
John Smith jsmith@hotmail.com (555)555-5555