Sql-server – How to store very small values (E-314) in SQL Server database

floating pointsql server

I had created table which hold small floating values using below query

create table temp_data (val float)

But when it gives warning message for very small values (e-314)

insert into temp_pce_rank_data values(2.466987305926227e-314)

Data gets inserted with value '0' warning message is: The floating point value '2.466987305926227e-314' is out of the range of computer representation (8 bytes).

How I can store & retirve very small values (E-314) in SQL Server database?
Thanks

Best Answer

None of the provided numeric types (float, decimal) are able to store so small numbers. The example you have, 2.466987305926227e-314, is smaller than the smallest normal double-precision float number.

I see two options, if you want to store such numbers:

  • Use a custom solution with two columns, one for the mantissa and another for the exponent. The disadvantage will be that you'll have to do all the math operators and functions with complex expressions (even for addition).

  • Use a custom CLR User-Defined Type. From that page:

Beginning with SQL Server 2005, you can use user-defined types (UDTs) to extend the scalar type system of the server, enabling storage of CLR objects in a SQL Server database. UDTs can contain multiple elements and can have behaviors, differentiating them from the traditional alias data types which consist of a single SQL Server system data type.

Because UDTs are accessed by the system as a whole, their use for complex data types may negatively impact performance. Complex data is generally best modeled using traditional rows and tables. UDTs in SQL Server are well suited to the following:

Date, time, currency, and extended numeric types

Geospatial applications

Encoded or encrypted data