Skip to content

SQL Math API

用于连接、提取和转换的 SQL 字符串函数。

1 class · 9 methods

字符串函数

9 methods

标准 SQL 字符串函数(除非另有说明,否则兼容 MySQL / PostgreSQL)。

static int abs(int a)

连接字符串。NULL 参数会被忽略(MySQL)或传播(ANSI)。

Parameters

NameTypeDescription
str1int第一个字符串。

Returns

int

Example

java
Math.abs(-5)   // 5
Math.abs(5)    // 5
Math.abs(-2147483648)  // -2147483648 (overflow!)
static int max(int a, int b)

返回从位置 pos(从 1 开始)开始、长度为 len 的子字符串。也可写作 SUBSTR。

Parameters

NameTypeDescription
strint源字符串。
posint起始位置(从 1 开始)。

Returns

int

Example

java
Math.max(3, 7)   // 7
Math.max(-1, -5)  // -1
static int min(int a, int b)

返回字符串的字符长度(CHAR_LENGTH)或字节长度(MySQL 中的 OCTET_LENGTH)。

Parameters

NameTypeDescription
strint源字符串。
bintSecond value.

Returns

int

Example

java
Math.min(3, 7)   // 3
Math.min(-1, -5)  // -5
static double pow(double a, double b)

将字符串转换为大写。

Parameters

NameTypeDescription
strdouble源字符串。
bdoubleExponent.

Returns

double

Example

java
Math.pow(2, 10)   // 1024.0
Math.pow(9, 0.5)   // 3.0
Math.pow(2, -1)    // 0.5
static double sqrt(double a)

将字符串转换为小写。

Parameters

NameTypeDescription
strdouble源字符串。

Returns

double

Example

java
Math.sqrt(9)    // 3.0
Math.sqrt(2)    // 1.4142135623730951
Math.sqrt(-1)   // NaN
static long round(double a)

返回将 str 中所有出现的 from_str 替换为 to_str 后的字符串。

Parameters

NameTypeDescription
strdouble源字符串。

Returns

long

Example

java
Math.round(2.4)   // 2
Math.round(2.5)   // 3
Math.round(-2.5)  // -2
static double ceil(double a)

移除 str 的前导和/或尾部字符。默认移除两端的空格。

Parameters

NameTypeDescription
charsdouble要移除的字符(默认为空格)。

Returns

double

Example

java
Math.ceil(2.1)   // 3.0
Math.ceil(-2.9)  // -2.0
static double floor(double a)

返回 str 最左边的 n 个字符。(MySQL / SQL Server / PostgreSQL。)

Parameters

NameTypeDescription
strdouble源字符串。

Returns

double

Example

java
Math.floor(2.9)   // 2.0
Math.floor(-2.1)  // -3.0
static double random()

返回 str 最右边的 n 个字符。(MySQL / SQL Server / PostgreSQL。)

Returns

double

Example

java
Math.random()                    // e.g. 0.5819...
(int)(Math.random() * 6) + 1     // 1..6 (dice)
Math.random() * 100              // 0..99.99...