I have a SQL row with type
summary NVARCHAR(4000) NULL
and before I add any data to the table I check it in code and fix it if the user has entered a string that is too long for the database using this code
if (instanceToCrop.Description != null && instanceToCrop.Description.Length > 4000) instanceToCrop.Description = instanceToCrop.Description.Substring(0, 4000);
If I change my table thus
ALTER TABLE Timeline
ALTER COLUMN summary NVARCHAR(MAX)
what constant should I use in place of 4000 to keep the submitted string below the length that the SQL server would reject?
The max number of characters you can store in an NVARCHAR(MAX) is 1,073,741,823 or very close to that. If your users can enter that amount of text in your textbox, there's probably something not optimal going on ... are they pasting The Bible or the transcript for all of the Lord of the Rings movies?
I think you can set a much more practical limit, but we can't really tell you what that should be... 50,000 characters? 100,000 characters? Something less than 64KB for network / packet reasons? Don't know, that seems to be a business decision, not a technical one.