Can you Limit an nvarchar(max) field in SQL

100 views Asked by At

I know this can be set in the client/server, but is there a way to put a character limit on an nvarchar(max) field over 4000 characters. IE 20,000 characters?

1

There are 1 answers

0
Stan On BEST ANSWER

As Sean hinted:

CREATE TABLE  ImageTable (ImageID INT IDENTITY PRIMARY KEY 
                      ,   ImageBinary VARBINARY(MAX)  )

ALTER TABLE [dbo].ImageTable ADD CONSTRAINT [CX_ImageTable_Size]  
                                    CHECK ( DATALENGTH(ImageBinary) < 19999)

INSERT INTO dbo.ImageTable (ImageBinary ) values (0x0000000000000000001)  
  --  throws error if length is longer than constraint