• Learning MySQL By Example
  • Introduction
  • 1. How to Retrieve Data From a Single Table
  • 2. How to Retrieve Data from Multiple Tables
  • 3. Using Functions
  • 4. How to Insert, Update, Delete Data in Tables
  • 5. Summary Queries and Aggregate Functions
  • 6. Working With Subqueries
  • 7. SQL Views
  • 8. SQL Indexes
  • Glossary
  • Index
  • Download
  • Translations
  • 3.2

    Numeric Functions

    ROUND

    • The ROUND function has two parameters. The first is a number, usually a DECIMAL or a FLOAT. The second defines the number of decimals to which the number will be rounded.
    • If no length is provided, the number is rounded to a whole number.

    Table 5. ROUND function

    Function Type Example Result

    ROUND(number[, length])

    Number

    ROUND(13.37, 1)

    13.4

    Example:

    USE world;
    SELECT name, LifeExpectancy, ROUND(LifeExpectancy) 
    FROM world.country;

    Results:

    05_functions.png

    FLOOR, CEILING, TRUNCATE

    • FLOOR() will return the next lowest whole number no matter what the decimal point.
    • CEILING() will return the next highest whole number no matter what the decimal point.
    • TRUNCATE() will return the number truncated to the precision specified.

    Table 6. FLOOR, CEILING, TRUNCATE functions

    Function Type Example Result

    FLOOR(number)

    number

    FLOOR(7.7)

    7

    CEILING(number)

    number

    CEILING(6.2)

    7

    TRUNCATE(NUMBER, length)

    number

    TRUNCATE(7.9)

    7

    Example:

    USE bike;
    SELECT list_price, FLOOR(list_price), CEILING(list_price),
        TRUNCATE(list_price, 0)
    FROM product;

    Results:

    06_functions.png

    This content is provided to you freely by EdTech Books.

    Access it online or download it at https://edtechbooks.org/learning_mysql/numeric_functions.