typestar

Numbers and rounding in SQL

Rounding is a decision, so make it an explicit one.

SELECT
    price,
    ROUND(price) AS nearest,
    CEIL(price) AS rounded_up,
    FLOOR(price) AS rounded_down,
    ABS(price - 100) AS distance_from_100,
    price % 10 AS remainder,
    ROUND(SQRT(price), 4) AS root,
    POWER(price, 2) AS squared
FROM products;

How it works

  1. ROUND takes decimals; CEIL and FLOOR always go one way.
  2. % is the remainder operator; MOD is the function spelling.
  3. ABS, SQRT and POWER do what their names say.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
232
Tokens
62
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 39 seconds.

Type this snippet

Step 5 of 7 in Expressions, step 20 of 24 in Analytics & reporting.

← Previous Next →