why varbinary instead of varchar [duplicate] why varbinary instead of varchar [duplicate] sql-server sql-server

why varbinary instead of varchar [duplicate]


Mediawiki changed from varchar to varbinary in early 2011:

War on varchar. Changed all occurrences of varchar(N) and varchar(N) binary to varbinary(N). varchars cause problems ("Invalid mix of collations" errors) on MySQL databases with certain configs, most notably the default MySQL config.


In MSSQL:

I think the big difference is only between nvarchar and varbinary.

Because nvarchar stores 2 bytes for each character instead of 1 byte.

varchar does the same as varbinary: from MSDN:

The storage size is the actual length of the data entered + 2 bytes" for both.

The difference here is by varbinary The data that is entered can be 0 bytes in length.

Here is a small example:

CREATE TABLE Test (textData varchar(255), binaryData varbinary(255))INSERT INTO Test VALUES('This is an example.', CONVERT(varbinary(255),'This is an example.',0))INSERT INTO Test VALUES('ÜŰÚÁÉÍä', CONVERT(varbinary(255),'ÜŰÚÁÉÍä',0))

What you can use here is the DATALENGTH function:

SELECT datalength(TextData), datalength(binaryData) FROM test

The result is 19 - 19 and 7 - 7

So in size they are the same, BUT there is an other difference. If you check the column specifications, you can see, that the varbinary (of course) has no collation and character set, so it could help use values from different type of encoding and character set easily.

SELECT   *FROM     INFORMATION_SCHEMA.COLUMNS WHERE     TABLE_NAME = 'Test' ORDER BY   ORDINAL_POSITION ASC;