字符串函数
9 methods标准 SQL 字符串函数(除非另有说明,否则兼容 MySQL / PostgreSQL)。
static int abs(int a)连接字符串。NULL 参数会被忽略(MySQL)或传播(ANSI)。
Parameters
| Name | Type | Description |
|---|---|---|
| str1 | int | 第一个字符串。 |
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
| Name | Type | Description |
|---|---|---|
| str | int | 源字符串。 |
| pos | int | 起始位置(从 1 开始)。 |
Returns
int
Example
java
Math.max(3, 7) // 7
Math.max(-1, -5) // -1static int min(int a, int b)返回字符串的字符长度(CHAR_LENGTH)或字节长度(MySQL 中的 OCTET_LENGTH)。
Parameters
| Name | Type | Description |
|---|---|---|
| str | int | 源字符串。 |
| b | int | Second value. |
Returns
int
Example
java
Math.min(3, 7) // 3
Math.min(-1, -5) // -5static double pow(double a, double b)将字符串转换为大写。
Parameters
| Name | Type | Description |
|---|---|---|
| str | double | 源字符串。 |
| b | double | Exponent. |
Returns
double
Example
java
Math.pow(2, 10) // 1024.0
Math.pow(9, 0.5) // 3.0
Math.pow(2, -1) // 0.5static double sqrt(double a)将字符串转换为小写。
Parameters
| Name | Type | Description |
|---|---|---|
| str | double | 源字符串。 |
Returns
double
Example
java
Math.sqrt(9) // 3.0
Math.sqrt(2) // 1.4142135623730951
Math.sqrt(-1) // NaNstatic long round(double a)返回将 str 中所有出现的 from_str 替换为 to_str 后的字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| str | double | 源字符串。 |
Returns
long
Example
java
Math.round(2.4) // 2
Math.round(2.5) // 3
Math.round(-2.5) // -2static double ceil(double a)移除 str 的前导和/或尾部字符。默认移除两端的空格。
Parameters
| Name | Type | Description |
|---|---|---|
| chars | double | 要移除的字符(默认为空格)。 |
Returns
double
Example
java
Math.ceil(2.1) // 3.0
Math.ceil(-2.9) // -2.0static double floor(double a)返回 str 最左边的 n 个字符。(MySQL / SQL Server / PostgreSQL。)
Parameters
| Name | Type | Description |
|---|---|---|
| str | double | 源字符串。 |
Returns
double
Example
java
Math.floor(2.9) // 2.0
Math.floor(-2.1) // -3.0static 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...