I have a query of SQL
that the content brings me like this:
[email protected];[email protected]
I need only the first address, I have to disregard everything that is from ;
?
I have a query of SQL
that the content brings me like this:
[email protected];[email protected]
I need only the first address, I have to disregard everything that is from ;
?
If it goes directly to SQL
use a function join, SUBSTRING and CHARINDEX :
SELECT SUBSTRING(email, 1,CHARINDEX(';', email)-1) from emails;
where email
is the field that stores the electronic addresses separated by commas and emails
the name of the table.
Retired response: < sub> SoEn - How to split a comma-separated value to columns
A suitability for picking up emails that do not have items separated by ;
, that is, only an email contained in the field without ;
:
select CASE WHEN Charindex(';', email)>0
THEN Substring(email, 1,Charindex(';', email)-1) ELSE
email END
from emails
There are a few ways:
with indexOf
String result = "[email protected];[email protected]";
int index = result.indexOf(";");
if (index > 0) {
result = result.substring(0, index);
}
System.out.println(result);
with split
String result = "[email protected];[email protected]";
String[] emails = result.split(";");
result = emails[0];
System.out.println(result);
You can use the Java split function to do this.
String email = "[email protected];[email protected]";
String[] emails = email.split(";");
System.out.println(emails[0]);
The split function divides a String into several Strings using the given delimiter and returns the parts in a String vector. As you need only the first, just use:
emails[0]
See working at ideone .