Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

sql statement to get before '@' from email address Expand / Collapse
Author
Message
Posted Friday, October 25, 2013 1:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Yesterday @ 9:48 AM
Points: 13, Visits: 65
i have email address column - pearson.charles@companyname.com
i want to show pearson.charles..anything before '@'
pls help
Post #1508580
Posted Friday, October 25, 2013 2:57 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 4:19 PM
Points: 188, Visits: 184
Hi Scuby,

Try this.

select REPLACE(SUBSTRING(Your_Column,1,CHARINDEX('@',Your_Column)),'@','') Name from Your_Table NOLOCK

Thanks
sibi.

Post #1508603
Posted Friday, October 25, 2013 3:07 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:50 AM
Points: 13,082, Visits: 12,547
Here are a couple of other ways.

;with MyEmail (email)
as
(
select 'pearson.charles@companyname.com'
)

select *
, LEFT(email, charindex('@', email, 0) - 1)
, SUBSTRING(email, 1, CHARINDEX('@', email) - 1)
, REPLACE(SUBSTRING(email,1,CHARINDEX('@',email)),'@','')
from MyEmail



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1508607
Posted Friday, October 25, 2013 3:08 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:50 AM
Points: 13,082, Visits: 12,547
p.shabbir (10/25/2013)
Hi Scuby,

Try this.

select REPLACE(SUBSTRING(Your_Column,1,CHARINDEX('@',Your_Column)),'@','') Name from Your_Table NOLOCK

Thanks
sibi.



Why NOLOCK???


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1508608
Posted Friday, October 25, 2013 3:25 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 4:19 PM
Points: 188, Visits: 184
Sean Lange (10/25/2013)
p.shabbir (10/25/2013)
Hi Scuby,

Try this.

select REPLACE(SUBSTRING(Your_Column,1,CHARINDEX('@',Your_Column)),'@','') Name from Your_Table NOLOCK

Thanks
sibi.



Why NOLOCK???


Sean,

At my work i used to specify "NOLOCK" at the end of select statement.Hence, by habit i mentioned there.You can take out that.
And i checked your query.
Only this is statement is working.
REPLACE(SUBSTRING(SEmail_Addr,1,CHARINDEX('@',SEmail_Addr)),'@','').

For other two got this error.
Error : Invalid length parameter passed to the LEFT or SUBSTRING function.

Post #1508613
Posted Friday, October 25, 2013 3:30 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 11:50 AM
Points: 13,082, Visits: 12,547
p.shabbir (10/25/2013)
Sean Lange (10/25/2013)
p.shabbir (10/25/2013)
Hi Scuby,

Try this.

select REPLACE(SUBSTRING(Your_Column,1,CHARINDEX('@',Your_Column)),'@','') Name from Your_Table NOLOCK

Thanks
sibi.



Why NOLOCK???


Sean,

At my work i used to specify "NOLOCK" at the end of select statement.Hence, by habit i mentioned there.You can take out that.


I would suggest that is a habit you learn to break unless you fully understand all the nasty stuff that hint brings to the table.

http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/10/1280.aspx

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/


And i checked your query.
Only this is statement is working.
REPLACE(SUBSTRING(SEmail_Addr,1,CHARINDEX('@',SEmail_Addr)),'@','').

For other two got this error.
Error : Invalid length parameter passed to the LEFT or SUBSTRING function.



That's odd. The code I posted works perfectly. Now if you have email values that don't have an @ it would not work where the find code you posted does. I merely posted a couple of alternative ways. In now way did I mean that your fine code was not sufficient. In fact, I had yours in my list of possibilities.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1508614
Posted Friday, October 25, 2013 3:50 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 4:19 PM
Points: 188, Visits: 184
Sean Lange (10/25/2013)
p.shabbir (10/25/2013)
Sean Lange (10/25/2013)
p.shabbir (10/25/2013)
Hi Scuby,

Try this.

select REPLACE(SUBSTRING(Your_Column,1,CHARINDEX('@',Your_Column)),'@','') Name from Your_Table NOLOCK

Thanks
sibi.



Why NOLOCK???


Sean,

At my work i used to specify "NOLOCK" at the end of select statement.Hence, by habit i mentioned there.You can take out that.


I would suggest that is a habit you learn to break unless you fully understand all the nasty stuff that hint brings to the table.

http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/11/10/1280.aspx

http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/


And i checked your query.
Only this is statement is working.
REPLACE(SUBSTRING(SEmail_Addr,1,CHARINDEX('@',SEmail_Addr)),'@','').

For other two got this error.
Error : Invalid length parameter passed to the LEFT or SUBSTRING function.



That's odd. The code I posted works perfectly. Now if you have email values that don't have an @ it would not work where the find code you posted does. I merely posted a couple of alternative ways. In now way did I mean that your fine code was not sufficient. In fact, I had yours in my list of possibilities.


Thanks for the links and your query is working perfectly.
I was running the statements individually.

Thanks.
Post #1508618
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse