cbrt
The cbrt() function calculates and returns the cube root of a given number. In mathematical terms, for a number x, its cube root y is determined by the equation y³ = x.
Syntax
The syntax for the cbrt() function is:
CBRT(number)
Where:
-
number: A required value representing the number for which to calculate the cube root. It can be a positive or negative whole number, a decimal, or even an expression that evaluates to a number.
For example, expressions like SELECT CBRT(some_column) from test_table, assuming some_column contains a numeric value.
|
Return Value:
- Returns |
Examples
These examples show how to use the cbrt() function:
Basic cube root calculation
Consider the example:
SELECT CBRT(125);
The result of this query will be:
cbrt
------
5
Cube root of a negative value
To calculate the cube root of a negative number, use the cbrt() function as shown:
SELECT CBRT(-125);
Result:
cbrt
------
-5
Cube root of decimal result
For calculations with decimal numbers:
SELECT CBRT(32);
The result is a decimal value:
cbrt
-------------------
3.174802103936399
Cube root of decimal input
In this scenario, a decimal value is provided as the argument:
SELECT CBRT(0.12815);
The result will be the cube root of the provided decimal value.
cbrt
------------
0.50416523
Handle incorrect argument
When a non-numeric argument is provided, the cbrt() function works as shown here:
SELECT CBRT('abc');
The function returns an error and the result is not valid.
invalid input syntax for type double precision: "abc"
CBRT operator (||/(x))
This example uses the CBRT operator (||/(x)) to calculate the cube root of a number:
SELECT ||/(1728) AS cbrt_operator;
This example calculates the cube root of 1728 using the CBRT operator. The result of this query will be:
cbrt_operator
--------------------
12.000000000000002