Returns the square root of a numeric expression that is greater than 0.
Syntax
double|decimal sqrt(<number>)Parameters
number: Required. Accepts DOUBLE or DECIMAL values. STRING and BIGINT values are implicitly converted to DOUBLE before evaluation. The value must be greater than 0; values that are 0 or negative return NULL.
Return value
The return type mirrors the input type:
DOUBLE input → DOUBLE
DECIMAL input → DECIMAL
STRING or BIGINT input → DOUBLE (after implicit type conversion)
NULL input → NULL
Examples
Static examples:
-- Returns 2.0
SELECT sqrt(4);
-- Returns NULL
SELECT sqrt(null);Column query:
Use the following sample table to test sqrt against different data types and edge cases:
CREATE TABLE IF NOT EXISTS mf_math_fun_t(
int_data INT,
bigint_data BIGINT,
double_data DOUBLE,
decimal_data DECIMAL,
float_data FLOAT,
string_data STRING
);
INSERT INTO mf_math_fun_t VALUES
(null, -10, 0.525, 0.525BD, CAST(0.525 AS FLOAT), '10'),
(-20, null, -0.1, -0.1BD, CAST(-0.1 AS FLOAT), '-10'),
(0, -1, null, 20.45BD, CAST(-1 AS FLOAT), '30'),
(-40, 4, 0.89, null, CAST(0.89 AS FLOAT), '-30'),
(5, -50, -1, -1BD, null, '50'),
(-60, 6, 1.5, 1.5BD, CAST(1.5 AS FLOAT), '-50'),
(-1, -70, -7.5, -7.5BD, CAST(-7.5 AS FLOAT), null),
(-80, 1, -10.2, -10.2BD, CAST(-10.2 AS FLOAT), '-1'),
(9, -90, 2.58, 2.58BD, CAST(2.58 AS FLOAT), '0'),
(-100, 10, -5.8, -5.8BD, CAST(-5.8 AS FLOAT), '-90');Calculate the square root across multiple typed columns:
SELECT
sqrt(bigint_data) AS bigint_new,
sqrt(double_data) AS double_new,
sqrt(decimal_data) AS decimal_new,
sqrt(string_data) AS string_new
FROM mf_math_fun_t;Result:
+--------------------+--------------------+-------------------+--------------------+
| bigint_new | double_new | decimal_new | string_new |
+--------------------+--------------------+-------------------+--------------------+
| NULL | 0.724568837309472 | 0.724568837309472 | 3.1622776601683795 |
| NULL | NULL | NULL | NULL |
| NULL | NULL | 4.522167621838006 | 5.477225575051661 |
| 2.0 | 0.9433981132056604 | NULL | NULL |
| NULL | NULL | NULL | 7.0710678118654755 |
| 2.449489742783178 | 1.224744871391589 | 1.224744871391589 | NULL |
| NULL | NULL | NULL | NULL |
| 1.0 | NULL | NULL | NULL |
| NULL | 1.606237840420901 | 1.606237840420901 | 0.0 |
| 3.1622776601683795 | NULL | NULL | NULL |
+--------------------+--------------------+-------------------+--------------------+NULL results appear for negative inputs and for string values that cannot be converted to a positive number.
Related functions
SQRT is a mathematical function. For other mathematical functions, see Mathematical functions.