Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Find a string

Author  Topic 

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2009-11-30 : 06:51:56
How do you extract one string from another? For example, how would you extract an email address from a sentence?

declare @s as varchar (500)
set @s='My email address is BobJones@mysite.com and that''s it.'


I want the select statement to return: BobJones@mysite.com.
But I need to indentify an email address first. All I know is that I'm looking for an email address. But I don't know what the email address is.
So I can look for the @ sign and then I can write a function to give me the location of the last space before the @ sign and work it out like that.
Is there a better way to do it? In one line? Without a function?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-30 : 07:14:58
See if this helps
http://sqlblogcasts.com/blogs/madhivanan/archive/2009/11/18/parsing-a-string.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2009-11-30 : 07:15:15
Maybe this is not the best way but this maybe one of the way.

DECLARE @x AS VARCHAR (500)
SET @x='My email address is BobJones@mysite.com and that''s it.'
DECLARE @xml AS XML
SET @xml= '<i>' + REPLACE(@x,' ','</i><i>') + '</i>'


SELECT * FROM
(
SELECT x.i.value('.','varchar(40)')AS mail FROM @xml.nodes('//i')x(i)
)t WHERE mail LIKE '%@%'

PBUH
Go to Top of Page

coolerbob
Aged Yak Warrior

841 Posts

Posted - 2009-11-30 : 08:18:15
not bad!

quote:
Originally posted by Idera

Maybe this is not the best way but this maybe one of the way.

DECLARE @x AS VARCHAR (500)
SET @x='My email address is BobJones@mysite.com and that''s it.'
DECLARE @xml AS XML
SET @xml= '<i>' + REPLACE(@x,' ','</i><i>') + '</i>'


SELECT * FROM
(
SELECT x.i.value('.','varchar(40)')AS mail FROM @xml.nodes('//i')x(i)
)t WHERE mail LIKE '%@%'

PBUH

Go to Top of Page
   

- Advertisement -