amesh
amesh

Reputation: 1319

Error converting data type varchar to varbinary. MSSQL when converting to Integer

create table hexvalue(Data varchar(10))

insert into hexvalue values('5'),('0E'),('12'),('17'),('15'),('EF'),('EF')

select convert(int, convert(varbinary, '0x'+Data, 1)) from hexvalue

This is throwing Error converting data type varchar to varbinary exception, except for '5'.

How can I fix this?

Upvotes: 0

Views: 4698

Answers (1)

Jaques
Jaques

Reputation: 2287

Hex always have two characters. Try this

create table hexvalue(Data varchar(10))

insert into hexvalue values('05'),('0E'),('12'),('17'),('15'),('EF'),('EF')

select convert(int, convert(varbinary, '0x'+Data, 1)) from hexvalue

Upvotes: 2

Related Questions