I have a varchar email field in my table. I can't figure out the sytnax 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.
+1
A:
Add a not null
and a check
constraint.
create table MyTable (
email varchar(100) not null,
constraint email_not_empty check (rtrim(ltrim(email)) != '')
)
Mehrdad Afshari
2009-03-13 19:04:39
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 Miller
2010-07-08 18:22:00
@Darrel: I don't think T-SQL supported TRIM.
Mehrdad Afshari
2010-07-09 07:24:54
No it doesn't but you could use either ltrim or rtrim and it should work.
Darrel Miller
2010-07-09 11:55:53
@Darrel: Oh, you're right. I misunderstood your comment.
Mehrdad Afshari
2010-07-09 11:59:38