October 6, 2010 at 9:13 am
In a SQL Server Stored Procedure, I am inserting fields from Active Directory into a table and need to use the email address field to populate my User ID field. I just need the characters before the @ sign in the email address.
Example:
I need to parse jwdoe from jwdoe@mydomain.com
This must be simple, but I am just learning!
Thanks.
October 6, 2010 at 9:18 am
Look at the CHARINDEX function and combine it with the LEFT, or SUBSTRING, function.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
October 6, 2010 at 9:35 am
try this
select SUBSTRING(EmailCOL, 1,CHARINDEX('@', EmailCol)-1) AS email from tableA
October 6, 2010 at 9:59 am
Thanks, this gives me the desired output but I received the following error message:
Invalid length parameter passed to the SUBSTRING function.
The email field contains some NULL values, could that be the problem?
October 6, 2010 at 10:20 am
jeboesch (10/6/2010)
Thanks, this gives me the desired output but I received the following error message:Invalid length parameter passed to the SUBSTRING function.
The email field contains some NULL values, could that be the problem?
A Null email address will return that error - if this is the case you have to add code to deal with those values either ignoring them or translating a null email address into something like "N/A"
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.October 7, 2010 at 6:11 am
jeboesch (10/6/2010)
The email field contains some NULL values, could that be the problem?
Elimitate NULLs either in the WHERE condition of the query (SELECT .... WHERE email_address IS NOT NULL) or replace the nulls using something like COALESCE(email_address, 'dummy@mydomain.com')
brgds
Philipp Post
October 7, 2010 at 6:39 am
Thanks everyone, I have it working now!
October 7, 2010 at 6:27 pm
This may be a silly question, but if you have access to Active directory, couldn't you get the username directly?
sAMAccountName...
I'm just thinking is someone has a different logon to their email address, eg: jo'connor@sample.com might be "joconnor" (without the apostrophe).
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply