I’m trying to store metric data (meters, kilometers, square-meters) in SQL Server 2012.
What is the best datatype to use? float (C#: double), decimal (C#: decimal) or even geometry? Or something different?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It completely depends on the application and what precision you need for it.
If we are talking about architecture then then precision needs are relatively limited and a
C# 32-bit floatwill take you a long way. In SQL this translates tofloat(24), also referred to as the database typereal. This SQL DB type requires 4 bytes of storage per entry.If we instead want to address points on the surface of the earth you need a lot higher precision. Here a
C# doubleis suitable, which corresponds to aSQL float(53)or justfloat. This SQL DB type requires 8 bytes of storage and should be used only if needed or if the application is small and disk/memory usage is not a concern.The
SQL Decimalis could be a good alternative for the actual SQL DB, but has 2 drawbacks:C# Decimalwhich is a type designed for financial usage and to prevent round-off errors. This design renders the C# Decimal type slower than a float/double when used in trigonometric methods etc. You could of course cast this back and forth in your code, but that is not the most straight-forward approach IMO.Lastly, here is a helpful resource for converting different types between .Net and SQL: SqlDbType Enumeration