?
Solved

Trim Everything After @ In Email Address

Posted on 2003-12-03
10
Medium Priority
?
719 Views
Last Modified: 2008-03-17
I am trying to write a statement that will take everything in an email address from the @ and all characters to its right off of the returned value.

i.e. - thisisatest@testing123456789.com  in this case the only data that will be returned would be

        thisisatest
0
Comment
Question by:H-SC
  • 4
  • 4
9 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 9866573
Hi H-SC,

Select Left(EMailAddress,CharIndex('@',EMailAddress) -1) As Address From MyTable

Is one way, this finds the position of the @ and then returns the left portion of the field.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 1

Author Comment

by:H-SC
ID: 9866653
Thanks for the quick response.

When I run that statement it gives me an error "Invalid param passed to the substring function"
error #536
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 9866694
The most likely situation is that one or more of the entries either contain no address or the address does not have the @ character in it. There are a number of ways to work around this, perhaps the easiest is to append an @ to the string in the first place which means you will get the entire address if no @ already present or the correct information if one is.

Select Left(EMailAddress + '@',CharIndex('@',EMailAddress + '@') -1) As Address From MyTable
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:H-SC
ID: 9866747
That is great.  I do not get errors on the last statement, however it only returns the first character in the email address.

i.e. - in my example of thisisatest@testing123456789.com would return "t"

any ideas?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 9866782
I am using this just to prove the case:

declare @EmailAddress varchar(50)
Set @emailaddress = 'thisisatest@testing123456789.com'
Select Left(@EMailAddress + '@',CharIndex('@',@EMailAddress + '@') -1) As Address

Which is returning the correct value.
0
 
LVL 1

Author Comment

by:H-SC
ID: 9866860
I ran the last one and it works.  Will the declare or set change any data or write back?
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 9866900
No not at all, this is unconnected to any table and just to demonstrate the principal. If the equivalent statement using your table and its fieldname(s) still fails then there must be something different with the data in the column that is causing this problem.
0
 
LVL 1

Author Comment

by:H-SC
ID: 9866935
GREAT!  will I need to do a different statement that will return real-time values.  The last one returned one row; "thisisatest"
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 9901094
Question closed and points refunded

PashaMod
CS Moderator
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question