Explain Codes LogoExplain Codes Logo

Rounding off to two decimal places in SQL

sql
precision
rounding
data-types
Nikita BarsukovbyNikita Barsukov·Jan 3, 2025
TLDR

Meticulously round numbers to two decimal places applying SQL's almighty ROUND() function:

-- SQL, you round my world; SELECT ROUND(column, 2) FROM table;

Replace column and table with your actual column and table names. The result is like running a comb through your data - tidy and elegant.

But don't stop here! Ensure uniform formatting by blending ROUND() with CAST():

-- Let the decimals sing in chorus; SELECT CAST(ROUND(column, 2) AS NUMERIC(16,2)) FROM table;

Now, your freshly minted golden data will always show two digits after the decimal point.

Present it right: The CAST function

CAST stars in this type converting show, dialing specific precision (up to 38) and scale:

-- Be precise, or be gone! SELECT CAST(column AS NUMERIC(precision, 2)) FROM table;

Here precision defines the total digit count, while scale is your personal DJ for digits after the decimal. For a wider range, consider NUMERIC(18,2) as your precision knight in the shinning armor.

FLOATing in precision with ROUND

When dealing with float-type data, precision is on the line. Always round before casting:

-- Round o' Float, you're next! SELECT CAST(ROUND(float_column, 2) AS FLOAT) FROM table;

FLOAT, being an approximate data type, can sometimes act strange. If you desire exact outcomes, then NUMERIC or DECIMAL are your soulmates.

SQL Server's inbuilt stylist: FORMAT

SQL Server 2012 or newer have FORMAT(), the inbuilt stylist for data:

-- One-liner beauty salon! SELECT FORMAT(column, 'N2') FROM table;

One simple line rounds and adds a consistent two decimal point display - the epitome of concise elegance.

Entertaining time-conversion with CAST

Converting minutes into hour, with a dab of CAST, follows the SQL Tao of precision:

-- Behold, the mathemagician! SELECT CAST((minutes_column / 60.0) AS NUMERIC(16,2)) FROM table;

By dividing by 60.0 (not just 60), we avoid the gruesome wrath of integer division.

Introduction to SQL aristocracy: Variables

Unravel the potential of declared variables in complex queries:

-- Welcome to the SQL Aristocracy!; DECLARE @ValueToRound NUMERIC(16,4) = column_name; SELECT CAST(ROUND(@ValueToRound, 2) AS NUMERIC(16,2));

Even Shakespeare would approve of this decl(i)aration.

Corner cases: SQL's quirky precision actors

Streamline precision and scale with these pro moves:

  • Save yourself from the embarrassment of overflow errors. Count x <= 38 in NUMERIC(x,2).
  • Review CAST settings with a fine-toothed comb. Leave no unwanted rounding undiscovered.
  • The fun-loving FLOAT type might lure you into rounding errors. Keep it real with DECIMAL for monetary values.

The DBMS detour: SQL Server, Oracle, PostgreSQL and others

This syntax specifically greets SQL Server inhabitants. For those in the kingdom of Oracle, PostgreSQL, MySQL and the like, there may be minor changes:

-- When in Rome.. SELECT ROUND(column_name, 2) FROM table;

Display beauty: The CAST and ROUND combo

Let layout speak louder than words. Combine ROUND() and CAST() for a beautiful data display:

-- That's some high fashion data!; SELECT CAST(ROUND(column, 2) AS NUMERIC(16,2)) FROM table;