Varbinary and image conversion

二次信任 提交于 2019-12-11 19:17:48

问题


I have an Access front end that links to a SQL Server backend.

There are 3 fields in a table that I am trying to convert to text from the backend:

o_name                varbinary(2000)  
O_PropertyBinary1     varbinary(2000)  
O_PropertyBinary2     image

I can convert the o_name field using:

convert(varchar([max]),[O_Name])  

and that works fine.

e.g. 4153534554  = ASSET

However, what can I use for the other two fields, as it seems I can't convert an image field and converting the O_PropertyBinary1 comes out with garbage characters.


回答1:


The output is depended on the stored data an the appropriate conversion.
If the stored data is binary e.g. Bitmaps, converting to text will never give a usable result.
If data stored is text, it could be Varchar or NVarchar and kind conversion is depending.

in the example below VC_VB2NVarchar and VC_IMG2NVarchar would display your described garbage characters

Declare @tab Table(nvc NVarchar(100),vc Varchar(100)
,img image,vb VarBinary(200),img2 image,vb2 VarBinary(200))
Insert into @tab (nvc,vc) Values ('123456789','123456789')

Update @tab set vb=Convert(VarBinary(200),nvc),img=Convert(Image,Convert(Varbinary(max),nvc))
               ,vb2=Convert(VarBinary(200),vc),img2=Convert(Image,Convert(Varbinary(max),vc))
Select nvc,vc
      ,CONVERT(Nvarchar(100),vb) as NVC_VB2NVarchar
      ,CONVERT(Varchar(200),vb)  as NVC_VB2Varchar
      ,CONVERT(Nvarchar(100),Convert(VarBinary(max),img)) as NVC_IMG2NVarchar
      ,CONVERT(Varchar(200),Convert(VarBinary(max),img))  as NVC_IMG2Varchar
      ,CONVERT(Nvarchar(100),vb2) as VC_VB2NVarchar
      ,CONVERT(Varchar(200),vb2)  as VC_VB2Varchar
      ,CONVERT(Nvarchar(100),Convert(VarBinary(max),img2)) as VC_IMG2NVarchar
      ,CONVERT(Varchar(200),Convert(VarBinary(max),img2))  as VC_IMG2Varchar

from @Tab      


来源:https://stackoverflow.com/questions/16409419/varbinary-and-image-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!