BigQuery reference
BigQuery mathematical functions
Mathematical functions round, divide, bucket and transform numbers. Watch the input type: INT64, NUMERIC and FLOAT64 behave differently on rounding, overflow and division by zero.
Functions in this category
| Function | What it does |
|---|---|
| ABS | ABS(x) returns the absolute value of x with the same type as the input: ABS(-42) is 42. |
| CEIL / CEILING | CEIL(x) returns the smallest whole number that is not less than x: CEIL(2.1) is 3.0 and CEIL(-2.9) is -2.0. |
| DIV | DIV(x, y) divides two integers and drops the remainder: DIV(7, 2) is 3, while 7 / 2 is 3.5. |
| FLOOR | FLOOR(x) returns the largest whole number that is not greater than x: FLOOR(2.9) is 2.0 and FLOOR(-2.1) is -3.0. |
| GREATEST | GREATEST(x1, x2, ...) returns the largest of its arguments within a single row, for example the latest of several date columns. |
| LEAST | LEAST(x1, x2, ...) returns the smallest of its arguments within a single row, such as the earliest of several dates or a value capped at a limit. |
| LOG, LN, LOG10 | LOG(x) returns the natural logarithm of x (the same as LN), LOG(x, base) uses any base, and LOG10(x) uses base 10. |
| MOD | MOD(x, y) returns the remainder of x divided by y, with the sign of x: MOD(25, 7) is 4 and MOD(-25, 7) is -4. |
| POW / POWER | POW(x, y) raises x to the power y, and POWER is a synonym: POW(2, 10) returns 1024.0. |
| RAND | RAND() returns a pseudo-random FLOAT64 from 0 (inclusive) to 1 (exclusive), different for every row and every run. |
| RANGE_BUCKET | RANGE_BUCKET(point, boundaries) returns the 0-based index of the bucket a value falls into, given a sorted array of boundaries: RANGE_BUCKET(25, [10, 20, 30]) is 2. |
| ROUND | ROUND(x, n) rounds x to n decimal places, with halfway cases going away from zero: ROUND(19.987, 2) returns 19.99. |
| SAFE_DIVIDE | SAFE_DIVIDE(x, y) divides x by y like the / operator but returns NULL instead of failing when y is 0 or the division overflows. |
| SQRT | SQRT(x) returns the square root of x: SQRT(16) is 4.0. |
| TRUNC | TRUNC(x, n) cuts x to n decimal places by rounding toward zero: TRUNC(2.789, 2) is 2.78 and TRUNC(-2.789, 2) is -2.78. |
More of the BigQuery reference
Date functionsDatetime functionsTimestamp functionsString functionsAggregate functionsWindow functionsArray functionsJSON functionsConversion functionsConditional expressionsHash and utility functionsQuery syntaxDDL statementsDML statementsScripting and data movementINFORMATION_SCHEMAError messagesBigQuery pricingPricing calculator
