简体   繁体   中英

Convert varchar(max) to varbinary(max)

I need to update a column using the convert binary function but it's throwing an error.

This is my query:

update DocumentOnline
set FileData = CONVERT(VARBINARY(max), FileData, 0x255044462D312E340A25E2E3CFD30A322030206F626A0A3C3C2F4C656E6774682034392F46696C7465722F466C6174654465636F64653E3E73747265616D0A789C2BE4720AE1323653B03030530849E1720DE10AE42A54305430004208999CABA01F9166A8E092AF10C80500EAA209F20A656E6473747265616D0A656E646F626A0A342030206F626A0A3C3C2F5265736F75726365733C3C2F584F626A6563743C3C2F586631203120) 
where ClientId = '54528' and EndDate = '201607'

and this is the error :

Argument data type varbinary(max) is invalid for argument 3 of convert function.

I'm a beginner in SQL - what am I missing ?

you are using wrong Syntax for CONVERT

https://msdn.microsoft.com/en-in/library/ms187928.aspx

-- Syntax for CONVERT:  
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )  

try to use this

update DocumentOnline
set FileData = CONVERT(VARBINARY(max),  0x255044462D312E340A25E2E3CFD30A322030206F626A0A3C3C2F4C656E6774682034392F46696C7465722F466C6174654465636F64653E3E73747265616D0A789C2BE4720AE1323653B03030530849E1720DE10AE42A54305430004208999CABA01F9166A8E092AF10C80500EAA209F20A656E6473747265616D0A656E646F626A0A342030206F626A0A3C3C2F5265736F75726365733C3C2F584F626A6563743C3C2F586631203120) 
where ClientId = '54528' and EndDate = '201607'

If data type of FileData is VARBINARY(MAX) just use this:

update DocumentOnline
set FileData = 0x255044462D312E340A25E2E3CFD30A322030206F626A0A3C3C2F4C656E6774682034392F46696C7465722F466C6174654465636F64653E3E73747265616D0A789C2BE4720AE1323653B03030530849E1720DE10AE42A54305430004208999CABA01F9166A8E092AF10C80500EAA209F20A656E6473747265616D0A656E646F626A0A342030206F626A0A3C3C2F5265736F75726365733C3C2F584F626A6563743C3C2F586631203120
where ClientId = '54528' and EndDate = '201607'

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