SQL Server provides several ways to format numbers, particularly when you need to control the number of decimal places displayed. This is crucial for presenting financial data, scientific measurements, or any numerical information where precision and readability are important. This tutorial will explore the common methods for achieving this, along with their nuances and best use cases.
Understanding Numeric Data Types
Before diving into formatting, it’s helpful to understand SQL Server’s numeric data types. INT
, BIGINT
, and SMALLINT
store whole numbers. FLOAT
and REAL
are approximate numeric data types. For precise decimal values, DECIMAL
and NUMERIC
are preferred. These types allow you to define the precision (total number of digits) and scale (number of digits to the right of the decimal point).
For example, DECIMAL(10, 2)
can store numbers with a total of 10 digits, with 2 digits after the decimal point. This provides a good balance between range and precision.
Using CONVERT
and CAST
The CONVERT
and CAST
functions can be used to change a numeric value to a different data type, and importantly, to specify formatting options, including the number of decimal places.
-
CONVERT
: Offers more formatting control and is typically preferred.SELECT CONVERT(DECIMAL(10, 2), 2.999999); -- Results in 3.00 SELECT CONVERT(DECIMAL(5, 2), 123.456); -- Results in 123.46 (truncates beyond defined precision)
Here,
DECIMAL(10, 2)
defines the target data type. SQL Server rounds the value to fit the specified precision and scale. -
CAST
: Provides a simpler way to change data types, but less formatting control.SELECT CAST(2.999999 AS DECIMAL(10, 2)); -- Results in 3.00
Using the FORMAT
Function (SQL Server 2012 and later)
For more flexible formatting options, the FORMAT
function is available in SQL Server 2012 and later versions.
DECLARE @test DECIMAL(18, 6) = 123.456789;
SELECT FORMAT(@test, '##.##'); -- Results in 123.46
The second argument, '##.##'
, is a format string:
#
represents a digit placeholder. It won’t display leading or trailing zeros if the value doesn’t require them..
indicates the decimal point.- The number of
#
symbols before and after the decimal point defines the maximum number of digits allowed in the integer and fractional parts respectively.
Using STR
Function
The STR
function can also be used for formatting, though it returns a string representation of the number. This can be useful when you need the output as a string for display purposes.
SELECT STR(12345.6789, 12, 3); -- Returns ' 12345.679' (includes leading spaces)
The arguments are: the number, the total length of the output string (including spaces), and the number of digits after the decimal point. Be aware that the result is a string with leading spaces to fill the specified total length.
Important Considerations
- Data Type: Formatting functions often return strings. If you need to perform further calculations, you might need to convert the formatted value back to a numeric data type.
- Rounding: Be aware of rounding behavior. SQL Server typically rounds values to fit the specified precision.
- Culture: The
FORMAT
function is culture-aware. The format string and the culture settings can affect the output. Specify a culture explicitly if you need consistent results across different systems. - Performance: Consider performance when choosing a formatting method, especially for large datasets.
CONVERT
andCAST
are generally faster thanFORMAT
.