Hi,
I have a field x varchar(6)
I want force the values at least at 4 char no less
how can i do it?
NULL must be still valid.
Thanks, FilippoUse a CHECK constraint:
CREATE TABLE #t(c1 varchar(6) NULL)
ALTER TABLE #t ADD CONSTRAINT cnstname CHECK (LEN(c1) >= 4)
GO
INSERT INTO #t (c1) VALUES('1234')
GO
INSERT INTO #t (c1) VALUES('123')
GO
INSERT INTO #t (c1) VALUES(NULL)
GO
SELECT * FROM #t
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Filippo Bettinaglio" <anonymous@.discussions.microsoft.com> wrote in message
news:1ed701c53e80$f4885ac0$a601280a@.phx.gbl...
> Hi,
> I have a field x varchar(6)
> I want force the values at least at 4 char no less
> how can i do it?
> NULL must be still valid.
> Thanks, Filippo|||Use a CHECK constraint:
ALTER TABLE Filippo
ADD CONSTRAINT CK_Filippo__len_x_gte_4
CHECK (LEN(x) >=4)
Note that the LEN of NULL is NULL, and NULL>=4 returns UNKNOWN, and that
doesn't violate the CHECK constraint. CHECK constraints (and constraints in
general) are only violated if the expression they check returns FALSE.
Jacco Schalkwijk
SQL Server MVP
"Filippo Bettinaglio" <anonymous@.discussions.microsoft.com> wrote in message
news:1ed701c53e80$f4885ac0$a601280a@.phx.gbl...
> Hi,
> I have a field x varchar(6)
> I want force the values at least at 4 char no less
> how can i do it?
> NULL must be still valid.
> Thanks, Filippo
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment