Solved

Trim Everything After @ In Email Address

Posted on 2003-12-03
10
697 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

752 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