如果’str’字符串非空并且末尾不包含’trailing_char’字符,则将’trailing_char’字符附加到末尾。
trailing_char只包含一个字符,如果包含多个字符,将返回NULL
MySQL [test_db]> select append_trailing_char_if_absent('a','c');
+------------------------------------------+
| append_trailing_char_if_absent('a', 'c') |
+------------------------------------------+
| ac |
+------------------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select append_trailing_char_if_absent('ac','c');
+-------------------------------------------+
| append_trailing_char_if_absent('ac', 'c') |
+-------------------------------------------+
| ac |
+-------------------------------------------+
1 row in set (0.01 sec)
返回字符串第一个字符对应的 ascii 码
MySQL [test_db]> select ascii('1');
+------------+
| ascii('1') |
+------------+
| 49 |
+------------+
1 row in set (0.01 sec)
MySQL [test_db]> select ascii('234');
+--------------+
| ascii('234') |
+--------------+
| 50 |
+--------------+
1 row in set (0.02 sec)
返回字符串的长度,对于多字节字符,返回字符数,目前仅支持utf8 编码。
这个函数还有一个别名 character_length。
MySQL [test_db]> select char_length("abc");
+--------------------+
| char_length('abc') |
+--------------------+
| 3 |
+--------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select char_length("中国");
+-----------------------+
| char_length('中国') |
+-----------------------+
| 2 |
+-----------------------+
1 row in set (0.01 sec)
将多个字符串连接起来,如果参数中任意一个值是 NULL,那么返回的结果就是 NULL
MySQL [test_db]> select concat("a", "b");
+------------------+
| concat('a', 'b') |
+------------------+
| ab |
+------------------+
1 row in set (0.00 sec)
MySQL [test_db]> select concat("a", "b", "c");
+-----------------------+
| concat('a', 'b', 'c') |
+-----------------------+
| abc |
+-----------------------+
1 row in set (0.00 sec)
MySQL [test_db]> select concat("a", null, "c");
+------------------------+
| concat('a', NULL, 'c') |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
使用第一个参数 sep 作为连接符,将第二个参数以及后续所有参数拼接成一个字符串,如果分隔符是 NULL,返回 NULL。
concat_ws函数不会跳过空字符串,会跳过 NULL 值
MySQL [test_db]> select concat_ws("or", "d", "is");
+----------------------------+
| concat_ws('or', 'd', 'is') |
+----------------------------+
| doris |
+----------------------------+
1 row in set (0.00 sec)
MySQL [test_db]> select concat_ws(NULL, "d", "is");
+----------------------------+
| concat_ws(NULL, 'd', 'is') |
+----------------------------+
| NULL |
+----------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select concat_ws("or", "d", NULL,"is");
+----------------------------------+
| concat_ws('or', 'd', NULL, 'is') |
+----------------------------------+
| doris |
+----------------------------------+
1 row in set (0.01 sec)
如果字符串以指定后缀结尾,返回1。否则,返回0。
任意参数为NULL,返回NULL。
MySQL [test_db]> select ends_with("Hello doris", "doris");
+-----------------------------------+
| ends_with('Hello doris', 'doris') |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select ends_with("Hello doris", "Hello");
+-----------------------------------+
| ends_with('Hello doris', 'Hello') |
+-----------------------------------+
| 0 |
+-----------------------------------+
1 row in set (0.01 sec)
返回 strlist 中第一次出现 str 的位置(从1开始计数)。
strlist 是用逗号分隔的字符串。如果没有找到,返回0。
任意参数为 NULL ,返回 NULL。
MySQL [test_db]> select find_in_set("b", "a,b,c");
+---------------------------+
| find_in_set('b', 'a,b,c') |
+---------------------------+
| 2 |
+---------------------------+
1 row in set (0.00 sec)
获取 key 为 “k1” 的 value
MySQL [test_db]> SELECT get_json_double('{"k1":1.3, "k2":"2"}', "$.k1");
+-------------------------------------------------+
| get_json_double('{"k1":1.3, "k2":"2"}', '$.k1') |
+-------------------------------------------------+
| 1.3 |
+-------------------------------------------------+
1 row in set (0.01 sec)
获取 key 为 “my.key” 的数组中第二个元素
MySQL [test_db]> SELECT get_json_double('{"k1":"v1", "my.key":[1.1, 2.2, 3.3]}', '$."my.key"[1]');
+---------------------------------------------------------------------------+
| get_json_double('{"k1":"v1", "my.key":[1.1, 2.2, 3.3]}', '$."my.key"[1]') |
+---------------------------------------------------------------------------+
| 2.2 |
+---------------------------------------------------------------------------+
1 row in set (0.01 sec)
获取二级路径为 k1.key -> k2 的数组中,第一个元素
MySQL [test_db]> SELECT get_json_double('{"k1.key":{"k2":[1.1, 2.2]}}', '$."k1.key".k2[0]');
+---------------------------------------------------------------------+
| get_json_double('{"k1.key":{"k2":[1.1, 2.2]}}', '$."k1.key".k2[0]') |
+---------------------------------------------------------------------+
| 1.1 |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)
获取 key 为 “k1” 的 value
MySQL [test_db]> SELECT get_json_int('{"k1":1, "k2":"2"}', "$.k1");
+--------------------------------------------+
| get_json_int('{"k1":1, "k2":"2"}', '$.k1') |
+--------------------------------------------+
| 1 |
+--------------------------------------------+
获取 key 为 “my.key” 的数组中第二个元素
MySQL [test_db]> SELECT get_json_int('{"k1":"v1", "my.key":[1, 2, 3]}', '$."my.key"[1]');
+------------------------------------------------------------------+
| get_json_int('{"k1":"v1", "my.key":[1, 2, 3]}', '$."my.key"[1]') |
+------------------------------------------------------------------+
| 2 |
+------------------------------------------------------------------+
1 row in set (0.01 sec)
获取二级路径为 k1.key -> k2 的数组中,第一个元素
MySQL [test_db]> SELECT get_json_int('{"k1.key":{"k2":[1, 2]}}', '$."k1.key".k2[0]');
+--------------------------------------------------------------+
| get_json_int('{"k1.key":{"k2":[1, 2]}}', '$."k1.key".k2[0]') |
+--------------------------------------------------------------+
| 1 |
+--------------------------------------------------------------+
1 row in set (0.01 sec)
获取 key 为 “k1” 的 value
MySQL [test_db]> SELECT get_json_string('{"k1":"v1", "k2":"v2"}', "$.k1");
+---------------------------------------------------+
| get_json_string('{"k1":"v1", "k2":"v2"}', '$.k1') |
+---------------------------------------------------+
| v1 |
+---------------------------------------------------+
1 row in set (0.01 sec)
获取 key 为 “my.key” 的数组中第二个元素
MySQL [test_db]> SELECT get_json_string('{"k1":"v1", "my.key":["e1", "e2", "e3"]}', '$."my.key"[1]');
+------------------------------------------------------------------------------+
| get_json_string('{"k1":"v1", "my.key":["e1", "e2", "e3"]}', '$."my.key"[1]') |
+------------------------------------------------------------------------------+
| e2 |
+------------------------------------------------------------------------------+
1 row in set (0.00 sec)
获取二级路径为 k1.key -> k2 的数组中,第一个元素
MySQL [test_db]> SELECT get_json_string('{"k1.key":{"k2":["v1", "v2"]}}', '$."k1.key".k2[0]');
+-----------------------------------------------------------------------+
| get_json_string('{"k1.key":{"k2":["v1", "v2"]}}', '$."k1.key".k2[0]') |
+-----------------------------------------------------------------------+
| v1 |
+-----------------------------------------------------------------------+
1 row in set (0.01 sec)
获取数组中,key 为 “k1” 的所有 value
MySQL [test_db]> SELECT get_json_string('[{"k1":"v1"}, {"k2":"v2"}, {"k1":"v3"}, {"k1":"v4"}]', '$.k1');
+---------------------------------------------------------------------------------+
| get_json_string('[{"k1":"v1"}, {"k2":"v2"}, {"k1":"v3"}, {"k1":"v4"}]', '$.k1') |
+---------------------------------------------------------------------------------+
| ["v1","v3","v4"] |
+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MySQL [test_db]> select city from table11;
+--------+
| city |
+--------+
| 北京 |
| 北京 |
| 上海 |
| 广州 |
| 深圳 |
+--------+
5 rows in set (0.01 sec)
MySQL [test_db]> select group_concat(city) from table11;
+----------------------------------------+
| group_concat(`city`) |
+----------------------------------------+
| 北京, 北京, 上海, 广州, 深圳 |
+----------------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select group_concat(city, " ") from table11;
+------------------------------------+
| group_concat(`city`, ' ') |
+------------------------------------+
| 北京 北京 上海 广州 深圳 |
+------------------------------------+
1 row in set (0.01 sec)
返回 substr 在 str 中第一次出现的位置(从1开始计数)。如果 substr 不在 str 中出现,则返回0。
MySQL [test_db]> select instr("abc", "b");
+-------------------+
| instr('abc', 'b') |
+-------------------+
| 2 |
+-------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select instr("abc", "d");
+-------------------+
| instr('abc', 'd') |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
与lower一致
它返回具有指定长度的字符串的左边部分,长度的单位为utf8字符
MySQL [test_db]> select left("Hello doris",5);
+------------------------+
| left('Hello doris', 5) |
+------------------------+
| Hello |
+------------------------+
1 row in set (0.01 sec)
返回字符串的字节。
MySQL [test_db]> select length("abc");
+---------------+
| length('abc') |
+---------------+
| 3 |
+---------------+
1 row in set (0.01 sec)
MySQL [test_db]> select length("中国");
+------------------+
| length('中国') |
+------------------+
| 6 |
+------------------+
1 row in set (0.01 sec)
返回 substr 在 str 中出现的位置(从1开始计数)。
如果指定第3个参数 pos,则从 str 以 pos 下标开始的字符串处开始查找 substr 出现的位置。如果没有找到,返回0
MySQL [test_db]> SELECT LOCATE('bar', 'foobarbar');
+----------------------------+
| locate('bar', 'foobarbar') |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)
MySQL [test_db]> SELECT LOCATE('xbar', 'foobar');
+--------------------------+
| locate('xbar', 'foobar') |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT LOCATE('bar', 'foobarbar', 5);
+-------------------------------+
| locate('bar', 'foobarbar', 5) |
+-------------------------------+
| 7 |
+-------------------------------+
1 row in set (0.00 sec)
将参数中所有的字符串都转换成小写
MySQL [test_db]> SELECT lower("AbC123");
+-----------------+
| lower('AbC123') |
+-----------------+
| abc123 |
+-----------------+
1 row in set (0.00 sec)
返回 str 中长度为 len(从首字母开始算起)的字符串。如果 len 大于 str 的长度,则在 str 的前面不断补充 pad 字符,直到该字符串的长度达到 len 为止。如果 len 小于 str 的长度,该函数相当于截断 str 字符串,只返回长度为 len 的字符串。
len 指的是字符长度而不是字节长度。
MySQL [test_db]> SELECT lpad("hi", 5, "xy");
+---------------------+
| lpad('hi', 5, 'xy') |
+---------------------+
| xyxhi |
+---------------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT lpad("hi", 1, "xy");
+---------------------+
| lpad('hi', 1, 'xy') |
+---------------------+
| h |
+---------------------+
1 row in set (0.01 sec)
将参数 str 中从开始部分连续出现的空格去掉
MySQL [test_db]> SELECT ltrim(' ab d');
+------------------+
| ltrim(' ab d') |
+------------------+
| ab d |
+------------------+
1 row in set (0.01 sec)
将数字按照货币格式输出,整数部分每隔3位用逗号分隔,小数部分保留2位
MySQL [test_db]> select money_format(17014116);
+------------------------+
| money_format(17014116) |
+------------------------+
| 17,014,116.00 |
+------------------------+
1 row in set (0.00 sec)
MySQL [test_db]> select money_format(1123.456);
+------------------------+
| money_format(1123.456) |
+------------------------+
| 1,123.46 |
+------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select money_format(1123.4);
+----------------------+
| money_format(1123.4) |
+----------------------+
| 1,123.40 |
+----------------------+
1 row in set (0.00 sec)
如果字符串为空字符串或者NULL,返回true。否则,返回false
MySQL [test_db]> select null_or_empty(null);
+---------------------+
| null_or_empty(NULL) |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.01 sec)
MySQL [test_db]>
MySQL [test_db]> select null_or_empty("");
+-------------------+
| null_or_empty('') |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.01 sec)
MySQL [test_db]>
MySQL [test_db]> select null_or_empty("a");
+--------------------+
| null_or_empty('a') |
+--------------------+
| 0 |
+--------------------+
对字符串 str 进行正则匹配,抽取符合 pattern 的第 pos 个匹配部分。
需要 pattern 完全匹配 str 中的某部分,这样才能返回 pattern 部分中需匹配部分。如果没有匹配,返回空字符串。
MySQL [test_db]> SELECT regexp_extract('AbCdE', '([[:lower:]]+)C([[:lower:]]+)', 1);
+-------------------------------------------------------------+
| regexp_extract('AbCdE', '([[:lower:]]+)C([[:lower:]]+)', 1) |
+-------------------------------------------------------------+
| b |
+-------------------------------------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT regexp_extract('AbCdE', '([[:lower:]]+)C([[:lower:]]+)', 2);
+-------------------------------------------------------------+
| regexp_extract('AbCdE', '([[:lower:]]+)C([[:lower:]]+)', 2) |
+-------------------------------------------------------------+
| d |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
对字符串 str 进行正则匹配, 将命中 pattern 的部分使用 repl 来进行替换
MySQL [test_db]> SELECT regexp_replace('a b c', " ", "-");
+-----------------------------------+
| regexp_replace('a b c', ' ', '-') |
+-----------------------------------+
| a-b-c |
+-----------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT regexp_replace('a b c','(b)','<\\1>');
+----------------------------------------+
| regexp_replace('a b c', '(b)', '<\1>') |
+----------------------------------------+
| a c |
+----------------------------------------+
1 row in set (0.01 sec)
将字符串 str 重复 count 次输出
count 小于1时返回空串
str,count 任一为NULL时,返回 NULL
MySQL [test_db]> SELECT repeat("a", 3);
+----------------+
| repeat('a', 3) |
+----------------+
| aaa |
+----------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT repeat("a", -1);
+-----------------+
| repeat('a', -1) |
+-----------------+
| |
+-----------------+
1 row in set (0.00 sec)
将字符串反转,返回的字符串的顺序和源字符串的顺序相反。
MySQL [test_db]> SELECT REVERSE('hello');
+------------------+
| reverse('hello') |
+------------------+
| olleh |
+------------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT REVERSE('你好');
+-------------------+
| reverse('你好') |
+-------------------+
| 好你 |
+-------------------+
1 row in set (0.01 sec)
它返回具有指定长度的字符串的右边部分, 长度的单位为utf8字符
MySQL [test_db]> SELECT right('Hello doris',5);
+-------------------------+
| right('Hello doris', 5) |
+-------------------------+
| doris |
+-------------------------+
1 row in set (0.01 sec)
返回 str 中长度为 len(从首字母开始算起)的字符串。如果 len 大于 str 的长度,则在 str 的后面不断补充 pad 字符,直到该字符串的长度达到 len 为止。如果 len 小于 str 的长度,该函数相当于截断 str 字符串,只返回长度为 len 的字符串。len 指的是字符长度而不是字节长度。’
MySQL [test_db]> SELECT rpad("hi", 5, "xy");
+---------------------+
| rpad('hi', 5, 'xy') |
+---------------------+
| hixyx |
+---------------------+
1 row in set (0.01 sec)
MySQL [test_db]> SELECT rpad("hi", 1, "xy");
+---------------------+
| rpad('hi', 1, 'xy') |
+---------------------+
| h |
+---------------------+
1 row in set (0.01 sec)
根据分割符拆分字符串,返回指定的分割部分(从1开始计数)。
MySQL [test_db]> select split_part("hello world", " ", 1);
+-----------------------------------+
| split_part('hello world', ' ', 1) |
+-----------------------------------+
| hello |
+-----------------------------------+
1 row in set (0.00 sec)
MySQL [test_db]> select split_part("hello world", " ", 2);
+-----------------------------------+
| split_part('hello world', ' ', 2) |
+-----------------------------------+
| world |
+-----------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select split_part("2019年7月8号", "月", 1);
+-----------------------------------------+
| split_part('2019年7月8号', '月', 1) |
+-----------------------------------------+
| 2019年7 |
+-----------------------------------------+
1 row in set (0.01 sec)
# 这里为空是因为 a的左边没有东西
MySQL [test_db]> select split_part("abca", "a", 1);
+----------------------------+
| split_part('abca', 'a', 1) |
+----------------------------+
| |
+----------------------------+
1 row in set (0.00 sec)
如果字符串以指定前缀开头,返回true。否则,返回false。任意参数为NULL,返回NULL。
MySQL [test_db]> select starts_with("hello world","hello");
+-------------------------------------+
| starts_with('hello world', 'hello') |
+-------------------------------------+
| 1 |
+-------------------------------------+
1 row in set (0.01 sec)
MySQL [test_db]> select starts_with("hello world","world");
+-------------------------------------+
| starts_with('hello world', 'world') |
+-------------------------------------+
| 0 |
+----------------------
它返回具有指定长度的字符串的左边部分,长度的单位为utf8字符
MySQL [test_db]> select strleft("Hello doris",5);
+---------------------------+
| strleft('Hello doris', 5) |
+---------------------------+
| Hello |
+---------------------------+
1 row in set (0.01 sec)
它返回具有指定长度的字符串的右边部分, 长度的单位为utf8字符
MySQL [test_db]> select strright("Hello doris",5);
+----------------------------+
| strright('Hello doris', 5) |
+----------------------------+
| doris |
+----------------------------+
1 row in set (0.01 sec)