What type of data to use when storing metric data in SQL databases?

I am trying to store metric data (meters, kilometers, square meters) in SQL Server 2012.

What is the best data type? float(C #: double), decimal(C #: decimal) or even geometry? Or something different?

+5
source share
3 answers

It completely depends on the application and what accuracy you need.

If we are talking about architecture, then the need for accuracy is relatively limited, and it C# 32-bit floatwill go a long way. In SQL, this means float(24)also called a database type real. This type of SQL DB requires 4 bytes of storage for each record.

, . a C# double , a SQL float(53) float. SQL DB 8 , / .

SQL Decimal SQL, 2 :

  • C# Decimal, , . # Decimal , float/double .. , , IMO.

    "The type of decimal value is suitable for financial calculations requiring a large number of significant integral and fractional digits and rounding errors." - MSDN: decimal structure

  • The SQL DB type Decimalrequires 5–9 bytes of storage per record (depending on the precision used), which is larger than the float (x) alternatives.
Therefore, use it according to your needs. In your comment, you declare that it is about real estate, so I would go for float(24)(aka real), which is exactly 4 bytes and translates directly to C# float. See: float and real (Transact-SQL)

Finally, here is a useful resource for converting different types between .Net and SQL: SqlDbType enumeration

+3
source

a decimal , int,

+4

Depends on what you want to make a float or double are inaccurate data types (therefore 5.0 == 5.0 may be false due to rounding problems)
Decimal is an exact data type (therefore 5.0 == 5.0 will always be true)
and geometry / geography (easy to say) are for locations on the map.

Calculation of the float should be a post among three, since geography is binary data with some information about the projection (here everything is about maps), and decimal is technically not as easy to process as a float.

+1
source

All Articles