1

In Sql Server I want To Convert this Value : 20E60E0175D4F44CD6F7947883DDD4D0

( Column Type Is NVARCHAR(MAX) )

To This Value : 0x20E60E0175D4F44CD6F7947883DDD4D0

( Column Type Is Binary(16) )

1 Answer 1

2

You can try like this:

DECLARE @test TABLE (
    nvar nvarchar(max),
    bin16 binary(16)
)

INSERT INTO @test (nvar) VALUES
(N'20E60E0175D4F44CD6F7947883DDD4D0')

UPDATE @test
SET bin16 = CONVERT(binary(16),'0x'+nvar,1)

SELECT *
FROM @test

Output:

nvar                                bin16
20E60E0175D4F44CD6F7947883DDD4D0    0x20E60E0175D4F44CD6F7947883DDD4D0

Note: there is more info about CONVERT and style = 1 in a context of binary datatype here on docs MS.

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

4 Comments

Also It Can Be Done Like This : DECLARE @bin BINARY(16) DECLARE valString NVARCHAR(MAX) SET valString = N'20E60E0175D4F44CD6F7947883DDD4D0' SET bin = CONVERT(binary(16),'0x'+valString,1) SELECT bin
My pleasure! If my answer was useful feel free to upvote/accept it!
It is quiet same way I introduced in my answer :) Good luck!
yes It Is But In My Query I Cant Define Temporary Table I Wrote It For Someone Like Myself tnks Again And Good Luck :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.