Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does varchar(max) = varchar?

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?

like image 636
Phillip Senn Avatar asked Dec 13 '22 21:12

Phillip Senn


2 Answers

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.

like image 72
LukeH Avatar answered Mar 13 '23 00:03

LukeH


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.

like image 20
prabhats.net Avatar answered Mar 12 '23 23:03

prabhats.net