Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need help with Charindex function..


Need help with Charindex function..

Author
Message
Sri8143
Sri8143
Old Hand
Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)

Group: General Forum Members
Points: 399 Visits: 2469
I am trying to capture an email address from the below message..whats the best possible way to do it ..


DECLARE @test VARCHAR(500)
SET @test = 'my email is Test@email.com.au how do i capture just the email'



This is what I have tried but couldn't get to finish it where I can capture the whole email address
SELECT SUBSTRING(@test,CHARINDEX('@',@test),CHARINDEX(' ',@test,CHARINDEX('@',@test))-CHARINDEX('@',@test))

Thanks in Advance

Thanks,
Chinna

Its the Journey which gives you Happiness not the Destination-- Dan Millman
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)SSCertifiable (7.5K reputation)

Group: General Forum Members
Points: 7466 Visits: 15142
Chinna

You need to REVERSE the string and then look for the first space after the "@".

John
parulprabu
parulprabu
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1454 Visits: 136
Hi,

Use this script


DECLARE @test VARCHAR(500)
SET @test = 'my email is Test@email.com.au how do i capture just the email'

SELECT REVERSE(SUBSTRING(REVERSE(@test),CHARINDEX('@',REVERSE(@test))+1,CHARINDEX(' ',REVERSE(@test),CHARINDEX('@',REVERSE(@test)))-CHARINDEX('@',REVERSE(@test))))
+SUBSTRING(@test,CHARINDEX('@',@test),CHARINDEX(' ',@test,CHARINDEX('@',@test))-CHARINDEX('@',@test))



ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8975 Visits: 19028
-- solution
;WITH SampleData AS (
SELECT test = 'my email is Test@email.com.au how do i capture just the email' UNION ALL
SELECT 'No email address in this string'
)
SELECT test, x3.p3
FROM SampleData
CROSS APPLY (SELECT p1 = NULLIF(CHARINDEX('@',test,1),0)) x1
CROSS APPLY (SELECT p2 = SUBSTRING(test,(2+p1-CHARINDEX(' ',REVERSE(LEFT(test, x1.p1)))),8000)) x2
CROSS APPLY (SELECT p3 = LEFT(p2,CHARINDEX(' ',p2,1)-1)) x3;

-- workings
;WITH SampleData AS (
SELECT test = 'my email is Test@email.com.au how do i capture just the email' UNION ALL
SELECT 'No email address in this string'
)
SELECT *
FROM SampleData
CROSS APPLY (SELECT p1 = NULLIF(CHARINDEX('@',test,1),0)) x1
CROSS APPLY (SELECT p2 = LEFT(test, x1.p1)) x2
CROSS APPLY (SELECT p3 = REVERSE(p2)) x3
CROSS APPLY (SELECT p4 = CHARINDEX(' ',p3)) x4
CROSS APPLY (SELECT p5 = 2+p1-p4) x5
CROSS APPLY (SELECT p6 = SUBSTRING(test,p5,8000)) x6
CROSS APPLY (SELECT p7 = LEFT(p6,CHARINDEX(' ',p6,1)-1)) x7;



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8548 Visits: 18137
Just another possible solution.
SELECT *
FROM DelimitedSplit8K(@test, ' ') x
WHERE CHARINDEX( '@', x.Item) > 0



For the code of DelimitedSplit8K please read the following article:
http://www.sqlservercentral.com/articles/Tally+Table/72993/


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 Visits: 6431
To add onto Luis's suggestion:


WITH SampleData AS (
SELECT test = 'my email is Test@email.com.au how do i capture just the email' UNION ALL
SELECT 'Some invalid@email addresses can also be removed like this' UNION ALL
SELECT 'Some invalid @email addresses can also be removed like this' UNION ALL
SELECT 'Some invalid email@. addresses can also be removed like this' UNION ALL
SELECT 'Some invalid email@@address.can can also be removed like this' UNION ALL
SELECT 'No email address in this string'
)
SELECT test, item
FROM SampleData a
CROSS APPLY dbo.DelimitedSplit8K(test, ' ') b
WHERE CHARINDEX('@', item) < CHARINDEX('.', item) AND CHARINDEX('@', item) > 1 AND
CHARINDEX('.', item) < LEN(item) AND LEN(item) - 1 = LEN(REPLACE(item, '@', ''));




Etc.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sri8143
Sri8143
Old Hand
Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)Old Hand (399 reputation)

Group: General Forum Members
Points: 399 Visits: 2469
Thanks a lot guys...All of the responses were helpful .

Thanks,
Chinna

Its the Journey which gives you Happiness not the Destination-- Dan Millman
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45119 Visits: 39918
Luis Cazares (9/30/2013)
Just another possible solution.
SELECT *
FROM DelimitedSplit8K(@test, ' ') x
WHERE CHARINDEX( '@', x.Item) > 0



For the code of DelimitedSplit8K please read the following article:
http://www.sqlservercentral.com/articles/Tally+Table/72993/


:-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search