Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Databricks SQL
Databricks Runtime 10.4 LTS and above
Returns the largest number not bigger than expr rounded down to targetScale digits relative to the decimal point.
Syntax
floor(expr [, targetScale])
Arguments
expr: An expression that evaluates to a numeric.targetScale: An optional INTEGER literal greater than-38specifying by how many digits after the decimal points to round down.
Returns
If no targetScale is given:
- If
exprisDECIMAL(p, s), returnsDECIMAL(p - s + 1, 0). - For all other cases, returns a BIGINT.
If targetScale is specified and expr is a:
TINYINTReturns a
DECIMAL(p, 0)withp = max(3, -targetScale + 1).SMALLINTReturns a
DECIMAL(p, 0)withp = max(5, -targetScale + 1).INTEGERReturns a
DECIMAL(p, 0)withp = max(10, -targetScale + 1)).BIGINTReturns a
DECIMAL(p, 0)withp = max(20, -targetScale + 1)).FLOATReturns a
DECIMAL(p, s)withp = max(14, -targetScale + 1))ands = min(7, max(0, targetScale))DOUBLEReturns a
DECIMAL(p, s)withp = max(30, -targetScale + 1))ands = min(15, max(0, targetScale))DECIMAL(p_in, s_in)Returns a
DECIMAL(p, s)withp = max(p_in - s_in + 1, -targetScale + 1))ands = min(s_in, max(0, targetScale))
If targetScale is negative the rounding occurs to -targetScale digits to the left of the decimal point.
The default targetScale is 0, which rounds down to the next smaller integral number.
When targetScale is specified with an integral input type, Azure Databricks raises ARITHMETIC_OVERFLOW if the result overflows the output type.
Warning
In Databricks Runtime, if spark.sql.ansi.enabled is false, an overflow does not cause an error but "wraps" the result instead.
Common error conditions
Examples
> SELECT floor(-0.1);
-1
> SELECT floor(5);
5
> SELECT floor(3345.1, -2);
3300
> SELECT floor(-12.345, 1);
-12.4
-- Rounding -128 to the nearest 10 produces -130, which overflows TINYINT.
> SELECT floor(cast(-128 AS TINYINT), -1);
Error: ARITHMETIC_OVERFLOW