Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trim Everything After @ In Email Address

Posted on 2003-12-03
10
Medium Priority
?
708 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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