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
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 |
- Example db<>fiddle