views:

69

answers:

3

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?

+7  A: 

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 and CONVERT functions, the default length is 30.

LukeH
Always a good idea to define the number of characters in a situation where you are casting. Or if you are defining a variable (where the default is 1 for varchar). This should just be second nature.
HLGEM
+2  A: 

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.

prabhats.net
Yes and varchar (Max) should not ever be used for fields that expect smaller data as it has indexing issues.
HLGEM
+2  A: 

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.

KM