1

I have a varchar email field in my table. I can't figure out the syntax to add a constraint that ensures a non-empty value cannot be inserted. I"m not talking about not null, I'm talking not empty here.

2 Answers 2

3

Add a not null and a check constraint.

create table MyTable (
   email varchar(100) not null,
   constraint email_not_empty check (rtrim(ltrim(email)) != '')
)
Sign up to request clarification or add additional context in comments.

3 Comments

Why do you need to ltrim and rtrim? If it only contains whitespace then a single trim should reduce the length to zero, no?
@Darrel: I don't think T-SQL supported TRIM.
No it doesn't but you could use either ltrim or rtrim and it should work.
0

len(ltrim(rtrim(email))) <> 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.