Explain Codes LogoExplain Codes Logo

What represents a double in sql server?

sql
data-types
decimal
precision
Anton ShumikhinbyAnton Shumikhin·Jan 26, 2025
TLDR

In SQL Server, the FLOAT data type mirrors a double from programming languages such as C#. It's handy for floating-point numbers where an approximation is tolerable.

Here's what it looks like in T-SQL:

-- Easiest way to get a double DECLARE @my_double FLOAT; -- double-precision -- For those who enjoy horrid nightmares DECLARE @my_terrifyingly_precise_double FLOAT(53); -- max precision, synonymous to a double

However, when your application doesn't tolerate approximations (like monetary operations or GPS data storage), you should consider using the DECIMAL data type.

Exactitude with DECIMAL

For specific, high-precision use-cases, we trot out the DECIMAL data type:

-- A decimal so precise, it might as well have a doctorate DECLARE @zoom_in_on_this DECIMAL(18,5);

DECIMAL provides the exactnumeric representation you need when precision is the goal. Consider it your go-to datatype for GPS coordinates, financial calculations, or any scenario where approximation just won't fly. However, exercise caution when handling data with companion .NET applications. Differences exist due to distinct binary representation.

Storing GPS Coordinates

Keep these in mind when choosing FLOAT or DECIMAL:

  • Accuracy: DECIMAL's fixed precision vs FLOAT's approximation
  • Efficiency: FLOAT's faster operations
  • Space: FLOAT uses less storage

A snazzy example of coordinate storage:

-- Coordinates are pushing the envelope on precision! DECLARE @fancy_latitude DECIMAL(9,6); DECLARE @fancy_longitude DECIMAL(9,6);

With DECIMAL(9,6) for latitude and longitude, you can accurately place a gnat on a map.

FLOAT(24) vs. FLOAT(53)

Size vs precision, a timeless struggle:

  • Space: FLOAT(24) uses less storage
  • Precision sacrifice: FLOAT(24) pay less, get less
  • Compatibility: Check your ecosystem needs

Rounding for User-Friendliness

Data presentation is as crucial as data accuracy. Prioritize readability and usability by rounding off values. Take for instance presenting latitude/longitude data:

-- "How many decimal places are we using?" "Yes." SELECT CAST(@Latitude AS DECIMAL(9,5)), CAST(@Longitude AS DECIMAL(9,5));

This strategy offers a user-friendly data presentation while retaining useful precision.