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
Add a not null and a check constraint.
create table MyTable (
email varchar(100) not null,
constraint email_not_empty check (rtrim(ltrim(email)) != '')
)
3 Comments
Darrel Miller
Why do you need to ltrim and rtrim? If it only contains whitespace then a single trim should reduce the length to zero, no?
Mehrdad Afshari
@Darrel: I don't think T-SQL supported TRIM.
Darrel Miller
No it doesn't but you could use either ltrim or rtrim and it should work.