Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a query to ensure email contains @

I'm creating a database in db2 and I want to add a constrain to validate whether the user inserting a valid email address, that contain %@%.% . Without luck...any advice?

like image 318
Mindan Avatar asked Oct 03 '13 08:10

Mindan


People also ask

How do I validate an email address in SQL?

Using the function like REGEXP_LIKE user can validate the email in MSSQL also. User can write different function in ms sql to validate mail like in oracle. AND PATINDEX('%[^a-z,0-9,@,.,_]%', REPLACE(email, '-', 'a')) = 0; The above function is used to validate the email address in Microsoft sql.

How do I add an email attribute in SQL?

you can use varchar as your data type for email column as emails are usually composed of letters, numbers and special characters. Show activity on this post. The right value of data lenght for the email field is database-agnostic.

How do you write an email query to an editor?

Steps Ensure the editor you are contacting accepts email query letters. Email the correct editor. Use an appropriate subject line. Use a formal salutation. Present your idea. Use the same format and information as you would in a written query, however try to make it more concise. Add a signature. Review your email before sending.

How to write a successful query letter?

Here are the nuts and bolts of how to write a successful query letter: Since you are corresponding with a human being, you’ll want to open the letter with a salutation, greeting them by name as you would any other letter. No matter how many of these query letters you email out, always personalize the letter to the particular agent.

How to use the and clause in a query?

Just be careful when using the 'or' statement. It's best to use parentheses around them such as select * from table where (field like '%a%' or field like '%b%'). If you have other AND clauses your query might act like SELECT * FROM TABLE WHERE (A = 1 AND B =2 AND C = 3) OR (D =4) which is usually not what you want.

Should I use contains or use SQL Server’s full text?

It seems like the most obvious choice to use, until you realize it requires some upfront work. Since CONTAINS relys on SQL’s full text capabilities, you’ll need to ensure those features are installed in the DBMS. Also, inorder to use on a column, the column requires a FULLTEXT index.


1 Answers

You can use LIKE with wildcards. See here for the wildcards on DB2.

The underscore character ( _ ) represents any single character.

The percent sign character (%) represents a string of zero or more characters.

SELECT email 
FROM YourTable 
WHERE email NOT LIKE '%_@__%.__%'

This will ignore the following cases (simple version for valid emails):

  • emails that have at least one character before the @;
  • emails that have at least two characters between @ and .;
  • emails that have at least two characters between . and the end.

You can see an example in MySql in sqlfiddle.

To add it as a constraint, you do (as mustaccio said in a comment):

alter table your_table add constraint chk_email check (email like '%_@__%.__%')
like image 91
Filipe Silva Avatar answered Oct 17 '22 17:10

Filipe Silva