Trim Everything After @ In Email Address

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. -  in this case the only data that will be returned would be

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
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.

Brainbench MVP for Visual Basic
H-SCAuthor Commented:
Thanks for the quick response.

When I run that statement it gives me an error "Invalid param passed to the substring function"
error #536
TimCotteeHead of Software ServicesCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

H-SCAuthor Commented:
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 would return "t"

any ideas?
TimCotteeHead of Software ServicesCommented:
I am using this just to prove the case:

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

Which is returning the correct value.
H-SCAuthor Commented:
I ran the last one and it works.  Will the declare or set change any data or write back?
TimCotteeHead of Software ServicesCommented:
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.
H-SCAuthor Commented:
GREAT!  will I need to do a different statement that will return real-time values.  The last one returned one row; "thisisatest"
Question closed and points refunded

CS Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.