October 25, 2013 at 1:25 pm
i have email address column - pearson.charles@companyname.com
i want to show pearson.charles..anything before '@'
pls help
October 25, 2013 at 2:57 pm
Hi Scuby,
Try this.
select REPLACE(SUBSTRING(Your_Column,1,CHARINDEX('@',Your_Column)),'@','') Name from Your_Table NOLOCK
Thanks
sibi.
October 25, 2013 at 3:07 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 25, 2013 at 3:08 pm
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 25, 2013 at 3:25 pm
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.
October 25, 2013 at 3:30 pm
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/[/url]
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 25, 2013 at 3:50 pm
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/[/url]
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.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply