SQL CBRT function
Returns the cube root of a number, defined for negatives too: CBRT(27) is 3 and CBRT(-27) is -3.
- カテゴリ
- Math
- Returns
double / float- In this category
- 24 of 82
構文
CBRT(number)
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | Not built in | No CBRT; POWER(x, 1.0/3.0) raises an invalid floating point operation for negative x |
| PostgreSQL | はい | PostgreSQL has cbrt(double precision) |
| MySQL | Not built in | No CBRT; POWER(-27, 1/3) raises 'DOUBLE value is out of range' |
| SQLite | Not built in | No CBRT in the 3.35+ math set; power(-27, 1.0/3) returns NULL |
| Oracle | Not built in | No CBRT; POWER(-27, 1/3) raises ORA-01428 |
例
PostgreSQL
SELECT CBRT(27) AS pos, CBRT(-27) AS neg; -- 3.0000000000000004, -3.0000000000000004
SQL Server
SELECT SIGN(-27) * POWER(ABS(CAST(-27 AS FLOAT)), 1e0/3) AS cbrt; -- -3
MySQL
SELECT SIGN(x) * POWER(ABS(x), 1e0/3) AS signed_root FROM t;
知っておくと便利なこと
- POWER(x, 1.0/3) has no real result for negative x: pow() refuses a negative base with a non-integer exponent. CBRT handles the sign explicitly.
- Emulate elsewhere with SIGN(x) * POWER(ABS(x), 1e0/3). Write the exponent as 1e0/3: on MySQL and SQL Server 1.0/3 is a short decimal (0.333333 on SQL Server), giving 2.99999. SQL Server POWER also returns its first argument's type, so cast an INT x to FLOAT.
- CBRT works in double precision, and PostgreSQL 18 returns 3.0000000000000004 for CBRT(27). Round before comparing to an integer.
Questions about CBRT
Why does POWER(-27, 1.0/3.0) fail?
A negative number raised to a non-integer power has no real result in pow(), so engines return NULL or raise an error instead of the -3 you expect. CBRT is the sign-aware fix.
Which engines have CBRT?
PostgreSQL only among the five. Elsewhere use SIGN(x) * POWER(ABS(x), 1e0/3), which keeps the sign of the original number. On SQL Server cast x to FLOAT so POWER does not return an integer.
Is CBRT(27) exactly 3?
Not on PostgreSQL 18, which returns 3.0000000000000004. CBRT works in double precision, so round before display or comparison.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.