soul shrestha
soul shrestha

Reputation: 125

issue on separating username and domain name from email address

I am using my sql to write sql query. So I have created a table called email. I wanted to separate the email into username and domain name.I made the use of substring and instring but it gives me error message as: Error Code: 1582. Incorrect parameter count in the call to native function 'Instr'

The Sql query I have used is below:

select substr(email,1,Instr(email,'@',1,1)-1) as username,
substr(email, Instr(email,'@',1,1)+1) as domainname
from email;

Could you please me out?

Upvotes: 1

Views: 728

Answers (1)

forpas
forpas

Reputation: 164139

The function instr() takes 2 arguments and not 4.
Change your code to this:

select 
  substr(email,1,Instr(email,'@')-1) as username, 
  substr(email, Instr(email,'@')+1) as domainname 
from email

You could also use substring_index():

select 
  substring_index(email, '@', 1) as username, 
  substring_index(email, '@', -1) as domainname 
from email

Upvotes: 1

Related Questions