1

Let say I have a varchar(100) call @hexString and I

SET @hexString ='0x47617279204a6f686e736f6e2032303230'

How do I cast this varchar to an actual hexidecimal 0x47617279204a6f686e736f6e2032303230(without the quotation mark)?

3
  • when you select this it wont show quotation mark Commented Nov 24, 2016 at 0:44
  • @mohan111 I know but I want to "cast" this varchar to a varbinary, like an actual sequence of hex numbers, instead of a string. Commented Nov 24, 2016 at 0:50
  • You want to cast that text without the presence of qoutation mark? Commented Nov 24, 2016 at 1:07

1 Answer 1

7

Use CONVERT with a "style" value of 1:

DECLARE @HexString VARCHAR(100);
SET @HexString = '0x47617279204a6f686e736f6e2032303230';

SELECT CONVERT(VARBINARY(100), @HexString, 1);

The "style" value is an optional 3rd parameter to the CONVERT function.

That returns the following as a VARBINARY type, not a string:

0x47617279204A6F686E736F6E2032303230
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.