Alexandru Severin
Alexandru Severin

Reputation: 6228

Minimum length constraint on a column

I'm trying to implement a minimum length constraint in Oracle.

As I read in this answer and multiple other similar questions I tried:

ALTER TABLE my_table 
ADD CONSTRAINT MY_TABLE_PASSWORD_CK CHECK (DATALENGTH(password) >=4)

And I am getting "DATALENGTH": invalid identifier". I also tried:

( DATALENGTH([password]) >=4 )
( LEN([password]) >=4 )
( LEN(password) >=4 )

What is the current format for this check constraint in Oracle?

Upvotes: 10

Views: 47127

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269703

DATALENGTH() returns the length in bytes in SQL Server. The equivalent Oracle function is LENGTHB() (documented here):

ALTER TABLE my_table
    ADD CONSTRAINT MY_TABLE_PASSWORD_CK CHECK (LENGTHB(password) >= 4)

However, for your purposes, I think the string length would be appropriate in both databases, LENGTH() in Oracle (or LEN() in SQL Server).

Upvotes: 21

Related Questions