0

I need to extract an email id from multiple id's given in a record. I am developing a tool by using C# to store data in to sql server and retrieve it using some logic. One of the logic is to extract desired (@gmail.com mentioned) email id from multiple mail id for a record.

Example, I need to extract xxx@gmail.com given record:

ID: 123

Name: abc
Email id: xxx@gmail.com xxx@yahoo.com

email id are separated by spaces.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
gvk
  • 1
  • 2
    I guess the obvious solution is not to store multiple values in one field in the first place. But if you *must* then you can use something like `string.Split()` to separate those values in your code and then choose the one you want. – David Nov 20 '15 at 12:18
  • Possible duplicate of [Best way to specify whitespace in a String.Split operation](http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation) – kayess Nov 20 '15 at 12:24
  • @David i hope string.Split() will solve my problem but what if those are not in same order like for one record there is a mail id gmail.com coming first and for other record yahoo.com is coming first.what ever the oder it might be. i need to extract only gmail.com irrespective of position. – gvk Nov 23 '15 at 10:59
  • @gvk: Then after you split the values, find the one which matches the conditions you're looking for. A "Where()" filter on the resulting enumeration should do that. The real point here is that you should fix the data. Storing multiple values in one column is just silly. – David Nov 23 '15 at 12:17

2 Answers2

0

Assuming you already have the list of emails separated from all the other data use string.split(null) to split the string containing all of the emails on whitespace. If you need further options the documentation for string.split() can be found here:

https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx

Tim B
  • 40,716
  • 16
  • 83
  • 128
0

Say these are two emails that belong to the same user:(Sice i don't know what's you specific case) You are much better off taking the user ID and storing the emails to separate table, and give both entries the user ID into another field. Then you can just do select * from email where email_user_id = users.user_id

If you already have the tables created then unfortunately you need to use yourString.split(' ') to split on Space, but a single space anywhere in the email other than the end of it, is going to break everything.

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
Иво Недев
  • 1,570
  • 1
  • 20
  • 33