简体   繁体   中英

Convert ntext to varbinary(max)

Is it possible to convert column values from ntext to varbinary(max) in SQL server 2000 R2 ?

Doing a workaround I found, initially I need to convert from ntext to nvarchar() then from nvarchar() to varbinary(max) .

No this is not a supported direct conversion.

As shown in the Data Type Conversion topic ( image ) you can only cast from ntext to one of the other string datatypes or to XML.

But you don't need to do this anyway.

The argument

Because i want the data to be stored in binary format.

doesn't make sense as it won't affect the physical storage anyway. See below that the actual bytes stored are the same.

CREATE TABLE T
(
A NVARCHAR(MAX),
B VARBINARY(MAX)
)

INSERT INTO T 
SELECT N'foobar',
       CAST(N'foobar' AS VARBINARY(MAX))

在此处输入图片说明

It just affects how the bytes are interpreted logically. Storing as varbinary datatype would make the data more difficult to work with (need to cast back to nvarchar to use as a string) and your application more fragile. Conversions between any data type and the binary data types are not guaranteed to be the same between versions of SQL Server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM