Numeric functions Stay organized with collections Save and categorize content based on your preferences.
Spanner supports the following MySQL numeric functions.You need to implement the MySQL functions in yourSpanner database before you can use them. For more information oninstalling the functions, seeInstall MySQL functions.
Function list
| Name | Summary |
|---|---|
mysql.DEGREES | Converts radians in to degrees. |
mysql.LOG2 | Returns the base-2 logarithm of the input parameter. ReturnsNULL if the input parameter is out of range. |
mysql.PI | Returns the value of pi (π). |
mysql.RADIANS | Converts degrees in to radians. |
mysql.TRUNCATE | Truncates an input parameter to a specified number of decimal places. |
mysql.DEGREES
mysql.DEGREES(numeric_expression)Description
Converts an angle value from radians to degrees.
This function supports the following argument:
numeric_expression: The angle in radians, specified as aFLOAT64value.
Return data type
FLOAT64
Limitations
If you provide a very large input value (greater than approximately 1e300), thefunction may result in an overflow error.
Example
The following example converts π radians to degrees:
SELECTmysql.DEGREES(ACOS(-1))aspi_in_degrees;/*+---------------+| pi_in_degrees |+---------------+| 180.0 |+---------------+*/mysql.LOG2
mysql.LOG2(numeric_expression)Description
Calculates the base-2 logarithm of a numeric value.
This function supports the following argument:
numeric_expression: TheFLOAT64value for which to calculate the base-2 logarithm.
Return data type
FLOAT64
Limitations
The input valuenumeric_expression must be greater than zero. Ifnumeric_expression is zero or negative, thefunction returnsNULL.
Example
The following example calculates the base-2 logarithm of 8:
SELECTmysql.LOG2(8)ASlog2_of_8;/*+-----------+| log2_of_8 |+-----------+| 3.0 |+-----------+*/mysql.PI
mysql.PI()Description
Returns the mathematical constant pi (π).
This function doesn't support any arguments.
Return data type
FLOAT64
Example
The following example returns the value of π:
SELECTmysql.PI()aspi_value;/*+-------------------+| pi_value |+-------------------+| 3.141592653589793 |+-------------------+*/mysql.RADIANS
mysql.RADIANS(numeric_expression)Description
Converts an angle value from degrees to radians.
This function supports the following argument:
numeric_expression: The angle in degrees, specified as aFLOAT64value.
Return data type
FLOAT64
Example
The following example converts 180 degrees to radians:
SELECTmysql.RADIANS(180)asradians_value;/*+-------------------+| radians_value |+-------------------+| 3.141592653589793 |+-------------------+*/mysql.TRUNCATE
mysql.TRUNCATE(numeric_expression,precision)Description
Truncates a number to a specified number of decimal places. This functiondoes not perform rounding.
This function supports the following arguments:
numeric_expression: TheFLOAT64value to truncate.precision: TheINT64value specifying the number of decimal places to preserve.Ifprecisionis positive, it truncates toprecisiondecimal places. Ifprecisionis zero, ittruncates to the nearest whole number towards zero. Ifprecisionis negative, itmakesprecisiondigits to the left of the decimal point zero.
Return data type
FLOAT64
Differences from MySQL
This function's behavior with two arguments,numeric_expression (the number) andprecision (the number of decimalplaces), is similar to MySQL'sTRUNCATE(numeric_expression, precision) function.
Limitations
TRUNCATE is a reserved keyword. If you use this function in Data DefinitionLanguage (DDL) statements, such as in generated column definitions, you mustenclose the function name in backticks (for example,mysql.`TRUNCATE`).
Example
The following example demonstrates various uses of theTRUNCATE function:
SELECTmysql.TRUNCATE(123.4567,2)astruncate_2_decimals,mysql.TRUNCATE(123.987,0)astruncate_to_integer,mysql.TRUNCATE(123.456,-1)astruncate_tens_place,mysql.TRUNCATE(-123.456,1)astruncate_negative_num;/*+---------------------+---------------------+---------------------+-----------------------+| truncate_2_decimals | truncate_to_integer | truncate_tens_place | truncate_negative_num |+---------------------+---------------------+---------------------+-----------------------+| 123.45 | 123.0 | 120.0 | -123.4 |+---------------------+---------------------+---------------------+-----------------------+*/Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.