Choose numeric data types
This article explains the numeric data types you can use when designing tables. It covers the exact number data types, both those which store whole numbers and those which allow decimal places, and the approximate float and real data types, including their ranges, storage sizes and when to choose each one.
Exact and approximate numbers
Numbers can be stored as exact numbers or as approximate (floating) numbers. Exact number data types are further divided into those which do not allow decimal numbers, and those which do.
Exact numbers without decimal places
The exact number data types which do not allow decimal numbers are:
| Data type | Range | Storage |
|---|---|---|
| bit | 0 to 1 | Up to 8 bits can be stored as 1 byte in total |
| tinyint | 0 to 255 | 1 byte |
| smallint | -32,768 to 32,767 | 2 bytes |
| int | -2,147,483,648 to 2,147,483,647 | 4 bytes |
| bigint | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 8 bytes |
Note
You can use COUNT for bit columns, but not SUM, AVG, MIN or MAX.
Integer literals and arithmetic
Number literals expressed without decimal places are converted into tinyint, smallint, int or decimal (not bigint).
You can use the following arithmetic operators:
+,-,*and/; and%(modulo – the remainder).
Important
An integer divided by an integer results in an integer, truncating any decimal places in the result.
Exact numbers with decimal places
The exact number data types which do allow for decimal numbers are decimal and numeric, and smallmoney and money.
decimal and numeric
decimal and numeric are synonyms. They can use two arguments:
-
Precision – the number of decimal digits to be stored, both before and after the decimal point, from 1 to 38.
- The default is 18.
- It needs 5 storage bytes, plus 4 additional bytes when the precision is at or over 10, 20 or 29.
-
Scale – the number of decimal places needed.
- This can only be up to the precision.
- The default is zero.
smallmoney and money
smallmoney and money are int and bigint divided by 10,000.
| Data type | Range | Storage |
|---|---|---|
| smallmoney | -214,748.3648 to 214,748.3647 | 4 bytes |
| money | -922,337,203,685,477.5808 to 922,337,203,685,477.5807 | 8 bytes |
Note
Currency information is not stored in the money and smallmoney data types.
Approximate numbers: float and real
Approximate numerics are stored in the float and real data types. The two possibilities are:
| Data type | Stores precisely | Storage | Range of digits |
|---|---|---|---|
| float(24), also known as real | Up to 7 digits | 4 bytes | Up to 38 digits to the left or right of the decimal point |
| float(53) | Up to 15 digits | 8 bytes | Up to 308 digits to the left or right of the decimal point |
After the precise number of digits, other digits are stored approximately.
Why use float instead of decimal or numeric?
You might use float:
- when you are using numbers where a close approximation is acceptable, and where the numbers stored are very large or very small;
- when storage costs need to be reduced. float(24) or real takes 4 bytes, but decimal starts at 5 bytes; and
- in the vector data type, when using AI embeddings. You can also use float16 with the vector data type, which only uses 2 bytes per value.
Comparing approximate numbers
Because float is approximate, exact comparisons are not really possible.
Important
Do not use the = or <> operators with float and real values; only use > and <.
Exam tip
Keep exact and approximate numbers distinct. decimal and numeric store exact values with a precision and scale you choose, while float and real store approximate values, so they suit very large or very small numbers where a close approximation is acceptable, but not exact comparisons.
Continue studying DP-800
This article covers Choose numeric data types, part of the Design and implement database objects section of the DP-800 study guide.