I noticed that I can write
SELECT CAST(Min(mynumber) AS VARCHAR(Max))+'mystring' AS X
as
SELECT CAST(Min(mynumber) AS VARCHAR)+'mystring' X
Will I regret leaving out the (Max) parameter?
I noticed that I can write
SELECT CAST(Min(mynumber) AS VARCHAR(Max))+'mystring' AS X
as
SELECT CAST(Min(mynumber) AS VARCHAR)+'mystring' X
Will I regret leaving out the (Max) parameter?
You'll regret it in the (unlikely) situation that MAX(mynumber)
has more than 30 characters:
When n is not specified when using the
CAST
andCONVERT
functions, the default length is 30.
VARCHAR(MAX) should be used for Large Objects.It uses the normal datapages until the content actually fills 8k of data. When overflow happens, data is stored as old TEXT, IMAGE and a pointer is replacing the old content. Varchar is for Variable-length, non-Unicode character data. n can be a value from 1 through 8,000. Max indicates that the maximum storage size is 2^31-1 bytes. Hope it helps.
When a varchar's lenght is not specified in a data definition or variable declaration statement, the default length is 1. When it is not specified when using the CAST and CONVERT functions, the default length is 30.
See: char and varchar (Transact-SQL)
I feel that it is poor practice to code without specifying a length for varchar.