Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What represents a double in sql server?

I have a couple of properties in C# which are double and I want to store these in a table in SQL Server, but noticed there is no double type, so what is best to use, decimal or float?

This will store latitude and longitude values, so I need the most accurate precision.

Thanks for the responses so far.

like image 979
Xaisoft Avatar asked Jul 30 '09 20:07

Xaisoft


People also ask

Whats a double in SQL?

DOUBLE(size, d) A normal-size floating point number. The total number of digits is specified in size. The number of digits after the decimal point is specified in the d parameter.

What does double mean in database?

They both represent floating point numbers. A FLOAT is for single-precision, while a DOUBLE is for double-precision numbers. MySQL uses four bytes for single-precision values and eight bytes for double-precision values.

What is the difference between decimal and double in SQL?

Decimal vs Double vs Float Precision is the main difference where float is a single precision (32 bit) floating point data type, double is a double precision (64 bit) floating point data type and decimal is a 128-bit floating point data type.

What does where 1/2 mean in SQL?

The reason you put the WHERE 1=2 clause in that SELECT INTO query is to create a field-copy of the existing table with no data. If you did this: select * into Table2 from Table1. Table2 would be an exact duplicate of Table1 , including the data rows.


2 Answers

float 

Or if you want to go old-school:

real 

You can also use float(53), but it means the same thing as float.

("real" is equivalent to float(24), not float/float(53).)

The decimal(x,y) SQL Server type is for when you want exact decimal numbers rather than floating point (which can be approximations). This is in contrast to the C# "decimal" data type, which is more like a 128-bit floating point number.

MSSQL's float type is equivalent to the 64-bit double type in .NET. (My original answer from 2011 said there could be a slight difference in mantissa, but I've tested this in 2020 and they appear to be 100% compatible in their binary representation of both very small and very large numbers -- see https://dotnetfiddle.net/wLX5Ox for my test).

To make things more confusing, a "float" in C# is only 32-bit, so it would be more equivalent in SQL to the real/float(24) type in MSSQL than float/float(53).

In your specific use case... All you need is 5 places after the decimal point to represent latitude and longitude within about one-meter precision, and you only need up to three digits before the decimal point for the degrees. Float(24) or decimal(8,5) will best fit your needs in MSSQL, and using float in C# is good enough, you don't need double. In fact, your users will probably thank you for rounding to 5 decimal places rather than having a bunch of insignificant digits coming along for the ride.

like image 163
richardtallent Avatar answered Oct 15 '22 12:10

richardtallent


Also, here is a good answer for SQL-CLR Type Mapping with a useful chart.

From that post (by David): enter image description here

like image 41
Mazdak Shojaie Avatar answered Oct 15 '22 12:10

Mazdak Shojaie