String Functions
Elastic Stack Serverless
用于执行字符串操作的函数。
ASCII(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: integer
描述:返回 string_exp 最左边字符的 ASCII 码值,结果是一个整数。
SELECT ASCII('Elastic');
ASCII('Elastic')
----------------
69
BIT_LENGTH(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: integer
描述:返回 string_exp 输入表达式的比特长度。
SELECT BIT_LENGTH('Elastic');
BIT_LENGTH('Elastic')
---------------------
56
CHAR(code)
输入:
- 介于
0和255之间的整数表达式。如果为null、负数或大于255,则函数返回null。
输出: string
描述:返回具有指定数字输入 ASCII 码值的字符。
SELECT CHAR(69);
CHAR(69)
---------------
E
CHAR_LENGTH(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: integer
描述:如果字符串表达式是字符数据类型,则返回输入的字符长度;否则,返回字符串表达式的字节长度(不小于比特数除以 8 的最小整数)。
SELECT CHAR_LENGTH('Elastic');
CHAR_LENGTH('Elastic')
----------------------
7
CONCAT(
string_exp1,
string_exp2)
输入:
- 字符串表达式。将
null视为空字符串。 - 字符串表达式。将
null视为空字符串。
输出: string
描述:返回一个字符串,该字符串是 string_exp1 连接 string_exp2 的结果。
结果字符串的最大字节长度不能超过 1 MB。
SELECT CONCAT('Elasticsearch', ' SQL');
CONCAT('Elasticsearch', ' SQL')
-------------------------------
Elasticsearch SQL
INSERT(
source,
start,
length,
replacement)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
null,则函数返回null。 - 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:返回一个字符串,该字符串从 source 中从 start 位置开始删除 length 个字符,并在 start 位置插入 replacement。
结果字符串的最大字节长度不能超过 1 MB。
SELECT INSERT('Elastic ', 8, 1, 'search');
INSERT('Elastic ', 8, 1, 'search')
----------------------------------
Elasticsearch
LCASE(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:返回与 string_exp 相等的字符串,并将所有大写字符转换为小写。
SELECT LCASE('Elastic');
LCASE('Elastic')
----------------
elastic
LEFT(
string_exp,
count)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
null,则函数返回null。如果为0或负数,则函数返回空字符串。
输出: string
描述:返回 string_exp 最左边的 count 个字符。
SELECT LEFT('Elastic',3);
LEFT('Elastic',3)
-----------------
Ela
LENGTH(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: integer
描述:返回 string_exp 中的字符数,不包括尾部空格。
SELECT LENGTH('Elastic ');
LENGTH('Elastic ')
--------------------
7
LOCATE(
pattern,
source
[, start]<3>
)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 字符串表达式。如果为
null,则函数返回null。 - 整数表达式;可选。如果为
null、0、1、负数或未指定,则搜索从第一个字符位置开始。
输出: integer
描述:返回 pattern 在 source 中首次出现的起始位置。可选的 start 参数指定开始搜索的字符位置。如果在 source 中未找到 pattern,则函数返回 0。
SELECT LOCATE('a', 'Elasticsearch');
LOCATE('a', 'Elasticsearch')
----------------------------
3
SELECT LOCATE('a', 'Elasticsearch', 5);
LOCATE('a', 'Elasticsearch', 5)
-------------------------------
10
LTRIM(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:返回 string_exp 中的字符,并删除前导空格。
SELECT LTRIM(' Elastic');
LTRIM(' Elastic')
-------------------
Elastic
OCTET_LENGTH(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: integer
描述:返回 string_exp 输入表达式的字节长度。
SELECT OCTET_LENGTH('Elastic');
OCTET_LENGTH('Elastic')
-----------------------
7
POSITION(
string_exp1,
string_exp2)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 字符串表达式。如果为
null,则函数返回null。
输出: integer
描述:返回 string_exp1 在 string_exp2 中的位置。结果是精确数值。
SELECT POSITION('Elastic', 'Elasticsearch');
POSITION('Elastic', 'Elasticsearch')
------------------------------------
1
REPEAT(
string_exp,
count)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
0、负数或null,则函数返回null。
输出: string
描述:返回一个由 string_exp 重复 count 次组成的字符串。
结果字符串的最大字节长度不能超过 1 MB。
SELECT REPEAT('La', 3);
REPEAT('La', 3)
----------------
LaLaLa
REPLACE(
source,
pattern,
replacement)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 字符串表达式。如果为
null,则函数返回null。 - 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:在 source 中搜索 pattern 的出现,并替换为 replacement。
结果字符串的最大字节长度不能超过 1 MB。
SELECT REPLACE('Elastic','El','Fant');
REPLACE('Elastic','El','Fant')
------------------------------
Fantastic
RIGHT(
string_exp,
count)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
null,则函数返回null。如果为0或负数,则函数返回空字符串。
输出: string
描述:返回 string_exp 最右边的 count 个字符。
SELECT RIGHT('Elastic',3);
RIGHT('Elastic',3)
------------------
tic
RTRIM(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:返回 string_exp 中的字符,并删除尾部空格。
SELECT RTRIM('Elastic ');
RTRIM('Elastic ')
-------------------
Elastic
SPACE(count)
输入:
- 整数表达式。如果为
null或负数,则函数返回null。
输出: string
描述:返回一个由 count 个空格组成的字符串。
结果字符串的最大字节长度不能超过 1 MB。
SELECT SPACE(3);
SPACE(3)
---------------
STARTS_WITH(
source,
pattern)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 字符串表达式。如果为
null,则函数返回null。
输出:布尔值
描述:如果源表达式以指定的模式开头,则返回 true,否则返回 false。匹配区分大小写。
SELECT STARTS_WITH('Elasticsearch', 'Elastic');
STARTS_WITH('Elasticsearch', 'Elastic')
--------------------------------
true
SELECT STARTS_WITH('Elasticsearch', 'ELASTIC');
STARTS_WITH('Elasticsearch', 'ELASTIC')
--------------------------------
false
SUBSTRING(
source,
start,
length)
输入:
- 字符串表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
null,则函数返回null。 - 整数表达式。如果为
null,则函数返回null。
输出: string
描述:返回一个字符串,该字符串从 source 中开始,从 start 指定的字符位置开始,提取 length 个字符。
SELECT SUBSTRING('Elasticsearch', 0, 7);
SUBSTRING('Elasticsearch', 0, 7)
--------------------------------
Elastic
TRIM(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:返回 string_exp 中的字符,并删除前导和尾部空格。
SELECT TRIM(' Elastic ') AS trimmed;
trimmed
--------------
Elastic
UCASE(string_exp)
输入:
- 字符串表达式。如果为
null,则函数返回null。
输出: string
描述:返回与输入相等的字符串,并将所有小写字符转换为大写。
SELECT UCASE('Elastic');
UCASE('Elastic')
----------------
ELASTIC