Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Storing binary data as nvarchar/varchar data in sql server

Is it possible to store binary data as varchar/nvarchar in SQL servers tables?

Example I am trying to store binary data 0x6806000004000000 as nvarchar data type but while trying to save to the tables values are getting blanked out. I want them to be stored as strings

Code used

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

declare @tempBinayToVarchar table (FinalValue NVARCHAR(60))
Insert into @tempBinayToVarchar
select Value from basetable 

Values in the table are values like:

0x000000000000000000000000000000000000000000000000000000000000004

>Solution :

If you’re trying to just store the 0x000... as a string without converting it to what the binary value actually represents, you can do that using CONVERT(0x000..., 1) though, for the specific value in your question, you need more than 60 characters.

DECLARE @tempBinayToNvarchar table (FinalValue nvarchar(120));

INSERT @tempBinayToNvarchar(FinalValue)
  SELECT CONVERT(nvarchar(120), 
    0x000000000000000000000000000000000000000000000000000000000000004, 
  1);
  
SELECT FinalValue FROM @tempBinayToNvarchar;

Output:

FinalValue
0x0000000000000000000000000000000000000000000000000000000000000004
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading