tags:

views:

66

answers:

2

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
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
@Darrel: I don't think T-SQL supported TRIM.
Mehrdad Afshari
No it doesn't but you could use either ltrim or rtrim and it should work.
Darrel Miller
@Darrel: Oh, you're right. I misunderstood your comment.
Mehrdad Afshari
A: 

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

jhale