简体   繁体   English

将nvarchar(max)转换为varbinary(max)

[英]Convert nvarchar(max) to varbinary(max)

Have table with values 有值表

report nvarchar(max) not null
description nvarchar(max) 

In stored procedure I want select values from table and then convert it to varbinary max. 在存储过程中,我想从表中选择值,然后将其转换为varbinary max。 I seletct : 我选:

select 
    CONVERT(varbinary(max), [report]) as [report], 
    ISNULL(CONVERT(varbinary(max), [description]), '') as [description]
from myTbl

but I get an error: 但我得到一个错误:

Implicit conversion from data type varchar to varbinary(max) is not allowed. 不允许从数据类型varchar隐式转换为varbinary(max)。 Use the CONVERT function to run this query. 使用CONVERT函数运行此查询。

Please help me to solve this problem 请帮我解决这个问题

The failure is occurring because you convert description to varbinary , but then try to cast any null values back to a varchar . 发生失败是因为您将描述转换为varbinary ,但随后尝试将所有null值varbinary转换回varchar You just need to move ISNULL inside the CONVERT or change the conversion value when null to a binary value. 您只需要在CONVERT移动ISNULL或将null转换为二进制值时将转换值更改即可。

ISNULL in CONVERT 在CONVERT中为ISNULL

SELECT 
    CONVERT(varbinary(MAX), report), 
    CONVERT(varbinary(max), ISNULL([description], '')) as [description]
FROM myTbl

Proper ISNULL Value 正确的ISNULL值

SELECT 
    CONVERT(varbinary(MAX), report), 
    ISNULL(CONVERT(varbinary(max), [description]), 0x) as [description]
FROM myTbl

Both versions will produce the same output 0x if description is null. 如果description为null,则两个版本将产生相同的输出0x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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