This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing an example of data conversion between Varbinary and Numeric data type in SQL Server.
This is just like any other data conversion, but whenever you convert from varbinary to numeric, you must be exact in the Precision & Scale. If this is wrong, it gives you the wrong result.
Convert From Numeric to Varbinary:
1 2 3 4 5 6 7 8 |
DECLARE @Numeric2 AS NUMERIC(8,4) SET @Numeric2 =8985.4563 SELECT CONVERT(VARBINARY(MAX),@Numeric2) AS Numeric_to_Varbinary -- Result: Numeric_to_Varbinary ---------------------- 0x0804000163125B05 |
Find Precision & Scale of this Varbinary Data:
1 2 3 4 5 6 7 8 9 10 11 |
SELECT CONVERT(INT,0x08) AS [Precision] SELECT CONVERT(INT,0x04) AS [Scale] -- Result: Precision ----------- 8 Scale ----------- 4 |
Convert From Varbinary to Numeric:
1 2 3 4 5 6 7 8 |
DECLARE @varbinary2 AS VARBINARY(MAX) SET @varbinary2=0x0804000163125B05 SELECT CONVERT(NUMERIC(8,4),@varbinary2) AS Varbinary_to_Numeric -- Result: Varbinary_to_Numeric ---------------------- 8985.4563 |