SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


sql statement to get before '@' from email address


sql statement to get before '@' from email address

Author
Message
scuby.me
scuby.me
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 84
i have email address column - pearson.charles@companyname.com
i want to show pearson.charles..anything before '@'
pls help
p.shabbir
p.shabbir
SSC-Addicted
SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)

Group: General Forum Members
Points: 461 Visits: 875
Hi Scuby,

Try this.

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

Thanks
sibi.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26382 Visits: 17557
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 Modens 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)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26382 Visits: 17557
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 Modens 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)
p.shabbir
p.shabbir
SSC-Addicted
SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)

Group: General Forum Members
Points: 461 Visits: 875
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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26382 Visits: 17557
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 Modens 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)
p.shabbir
p.shabbir
SSC-Addicted
SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)SSC-Addicted (461 reputation)

Group: General Forum Members
Points: 461 Visits: 875
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.
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