Solved

Trim Everything After @ In Email Address

Posted on 2003-12-03
10
658 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
10 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
 
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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now