I have a table which has a VARCHAR(MAX)
column, and I need to change it to VARBINARY(MAX)
.
I tried using the command
ALTER TABLE TableName ALTER COLUMN ColumnName VARBINARY(MAX)
but I got the error
Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type varchar(max) to varbinary(max) is not allowed.
Use the CONVERT function to run this query.
The table has no data, so I can't understand why it's complaining about data conversion.
Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query.
The VARBINARY type is similar to the VARCHAR type, but stores binary byte strings rather than non-binary character strings.
VARCHAR(MAX) is different from VARCHAR because it supports character strings up to 2 GB (2,147,483,647 bytes) in length. You should consider using VARCHAR(MAX) only when each string stored in this data type varies considerably in length, and a value might exceed 8000 bytes in size.
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section 8.4.
You cannot perform this conversion using an ALTER TABLE
statement since converting from varchar(max)
to varbinary(max)
requires an explicit conversion. So you should follow these steps to alter your table:
VARBINARY(MAX)
VARCHAR(MAX)
column, use update statement to add the data to the VARBINARY
columnVARCHAR(MAX)
columnvarbinary
column to varchar
name (per comment from @Ben Thul)Convert Varchar
to Int
and then change Int
to Binary
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With