文档

字符串函数

更新时间:
一键部署
  • CHR:以字符串形式返回整数N对应的Unicode编码。

  • CONCAT:字符串连接操作,其中任何一个参数为null,则返回值为null

  • LENGTH:返回字符串str的长度。

  • LOWER/LCASE:将字符串str中的字母转换为小写。

  • LPAD:将字符串str左边拼接padstr直到长度达到len,并返回拼接后的字符串。

  • LTRIM:删除字符串str所有前导空格。

  • REPLACE:将str中的from_str内容替换为to_str

  • RIGHT:返回字符串str中最右边的len个字符。

  • LEFT:返回字符串str中最左边的len个字符。

  • REVERSE:返回str逆序后的字符串。

  • RPAD:将字符串str右边拼接padstr直到长度达到len,并返回拼接后的字符串。

  • RTRIM:删除字符串str的所有后置空格。

  • SPLIT:将字符串按分隔符delimiter进行分隔,并返回数组。

  • STRPOS:返回字符串str中子字符串substr首次出现的位置,位置从1开始 ,如果未找到则返回0。

  • SUBSTR/SUBSTRING:返回指定子串。

  • UPPER/UCASE:将字符串str中的字母转换为大写。

  • NORMALIZE:使用NFC规范化形式返回字符串。

  • TO_UTF8:返回字符串的UTF-8编码格式。

  • ASCII:返回字符str或者字符串str最左边字符对应的十进制ASCII值。

  • BIN:返回N的二进制字符串。

  • CHAR:返回整数N1N2…对应的十进制ASCII码组成的字符串。

  • CHAR_LENGTH/CHARACTER_LENGTH:以字符为单位返回字符串str的长度。

  • EXPORT_SET:返回一个字符串,根据整数bits的二进制位01值,从右到左(从低位到高位)放置onoff字符串。1的位置放on字符串,0的位置放off字符串,由分隔符字符串(默认为逗号字符)分隔。 检查位数由number_of_bits指定,如果未指定,默认值为64

  • FIND_IN_SET:返回str在列表strlist中的位置。

  • FORMAT:将数字X格式化为#,###,###.##样式,舍入到D小数位,并将结果作为字符串返回。

  • FROM_BASE64:解码Base64编码过的字符串,并以BINARY类型输出。

  • HEX:返回整数N所对应的十六进制字符串,或者返回str中每个字符对应的十六进制数所组成的字符串。

  • PRESTO_INSERT:将str中从pos位置开始、长度为len的字符串替换为newstr

  • INSTR:返回字符串str中子字符串substr首次出现的位置。

  • LOCATE:返回字符串str中首次出现substr的位置信息,或者返回字符串str中从指定位置pos开始首次出现substr的位置信息。

  • MID:与SUBSTRING功能相同,从字符串strpos开始返回len长度的子字符串。

  • OCT:返回整数N的八进制字符串表示形式。

  • ORD:返回参数最左侧字符的字符编码。

  • REPEAT:返回由字符串str重复count次数组成的字符串。

  • SOUNDEX:返回参数str的SOUNDEX字符串。

  • SPACE:返回由指定数量空格组成的字符串。

  • SUBSTRING_INDEX:返回str中分隔符delimitercount次出现之前的子字符串。

  • INITCAP:将字符串的首字母转换为大写,其它字母均为小写。

  • REGEXP_COUNT:在源字符串source_char中搜索正则表达式pattern,并返回该表达式在字符串中出现的次数。如果未找到匹配项,将返回0。

  • REGEXP_SUBSTR:在源字符串source_char中从position位置搜索符合正则表达式pattern的子字符串。

  • TRANSLATE:将exprfrom_string中的每个字符都被替换为to_string中的相应字符。

CHR

  1. chr(bigint N);
  • 命令说明: 以字符串形式返回整数N对应的Unicode编码。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select chr(65);
    2. +---------+
    3. | chr(65) |
    4. +---------+
    5. | A |

CONCAT

  1. concat(string str1, …, string strn)
  • 命令说明:字符串连接操作,其中任何一个参数为null,则返回值为null

  • 返回值类型:VARCHAR。

  • 示例:

    1. select concat('Data', ', ', 'Lake');
    2. +------------------------------+
    3. | concat('Data', ', ', 'Lake') |
    4. +------------------------------+
    5. | Data, Lake |
    1. select concat('abc',null,'def');
    2. +----------------------------+
    3. | concat('abc', null, 'def') |
    4. +----------------------------+
    5. | NULL |

LENGTH

  1. length(string str)
  • 命令说明:返回字符串str的长度。

  • 返回值类型:BIGINT。

  • 示例:

    1. select length('aliyun');
    2. +------------------+
    3. | length('aliyun') |
    4. +------------------+
    5. | 6 |

LOWER/LCASE

  1. lower(string str)
  • 命令说明:将字符串str中的字母转换为小写。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select lower('Aliyun');
    2. +-----------------+
    3. | lower('Aliyun') |
    4. +-----------------+
    5. | aliyun |

LPAD

  1. lpad(string str, bigint len, string padstr)
  • 命令说明:将字符串str左边拼接padstr直到长度达到len,并返回拼接后的字符串。

    如果str长于len,则返回值将缩短为len个字符。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select lpad('Aliyun',9,'#');
    2. +------------------------+
    3. | lpad('Aliyun', 9, '#') |
    4. +------------------------+
    5. | ###Aliyun |

LTRIM

  1. ltrim(string str)
  • 命令说明:删除字符串str所有前导空格。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select ltrim(' abc');
    2. +----------------+
    3. | ltrim(' abc') |
    4. +----------------+
    5. | abc |

REPLACE

  1. replace(string str, string from_str, string to_str)
  • 命令说明:将str中的from_str内容替换为to_str

  • 返回值类型:VARCHAR。

  • 示例:

    1. select replace('WWW.aliyun.com', 'W', 'w');
    2. +-------------------------------------+
    3. | replace('WWW.aliyun.com', 'W', 'w') |
    4. +-------------------------------------+
    5. | www.aliyun.com |

RIGHT

  1. right(string str, bigint len)
  • 命令说明:返回字符串str中最右边的len个字符。

    如果str或者lennull,返回结果为null

  • 返回值类型:VARCHAR。

  • 示例:

    1. select right('abc',3);
    2. +---------------------+
    3. | right('abc', 3) |
    4. +---------------------+
    5. | abc |

LEFT

  1. left(string str, bigint len)
  • 命令说明:返回字符串str中最左边的len个字符。

    如果str或者lennull,则返回结果为null

  • 返回值类型:VARCHAR。

  • 示例:

    1. select left('foobarbar', 5);
    2. +-----------------------------+
    3. | left('foobarbar', 5) |
    4. +-----------------------------+
    5. | fooba |

REVERSE

  1. reverse(string str)
  • 命令说明:返回str逆序后的字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select reverse('123456');
    2. +-------------------+
    3. | reverse('123456') |
    4. +-------------------+
    5. | 654321 |

RPAD

  1. rpad(string str, bigint len, string padstr)
  • 命令说明:将字符串str右边拼接padstr直到长度达到len,并返回拼接后的字符串。

    如果str长于len,则返回值将缩短为len个字符。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select rpad('Aliyun',9,'#');
    2. +------------------------+
    3. | rpad('Aliyun', 9, '#') |
    4. +------------------------+
    5. | Aliyun### |

RTRIM

  1. rtrim(string str)
  • 命令说明:删除字符串str的所有后置空格。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select rtrim('barbar ');
    2. +--------------------+
    3. | rtrim('barbar ') |
    4. +--------------------+
    5. | barbar |

SPLIT

  1. split(string str, delimiter)
  2. split(string str, delimiter, limit)
  3. split_part(string str, delimiter, index)
  4. split_to_map(string str, entryDelimiter, keyValueDelimiter)
  • 命令说明:

    • split(string str, delimiter)将字符串str按分隔符delimiter进行分隔,并返回数组。

    • split(string str, delimiter, limit)将字符串str按分隔符delimiter分隔,并返回按limit大小限制的数组,且limit为正数。数组中的最后一个元素包含字符串中的所有剩余内容。

    • split_part(string str, delimiter, index)将字符串str按分隔符delimiter分隔,并返回分隔后数组下标为index的子串,index以1开头,如果大于字段数则返回null。

    • split_to_map(string str, entryDelimiter, keyValueDelimiter)通过entryDelimiter和keyValueDelimiter拆分字符串str并返回map。entryDelimiter将字符串分解为key-value对,keyValueDelimiter将每对分隔成key、value。

  • 返回值类型:VARCHAR或map<varchar, varchar>

  • 示例:

  1. select split('1#2#3', '#'), split('#1#2#3#', '#'),
  2. split('123', '#'), split('1#2#3', '#', 2);
  3. | _col0 | _col1 | _col2 | _col3 |
  4. +-----------+---------------+-------+----------+
  5. | [1, 2, 3] | [, 1, 2, 3, ] | [123] | [1, 2#3] |
  6. select split_part('A#B#C', '#', 2),
  7. split_part('A#B#C', '#', 4);
  8. +---------------------+-------------------------+
  9. | _col0 | _col1 |
  10. +---------------------+-------------------------+
  11. | B | NULL |
  12. select split_to_map('k1:v1,k2:v2', ',', ':'),
  13. split_to_map('', ',', ':');
  14. +---------------------+-------------------------+
  15. | _col0 | _col1 |
  16. +---------------------+-------------------------+
  17. | {k1=v1, k2=v2} | {} |

STRPOS

  1. strpos(string str, string substr)
  • 命令说明:返回字符串str中子字符串substr首次出现的起始位置,位置从1开始 ,如果未找到则返回0。

  • 返回值类型:BIGINT

  • 示例:

    1. select strpos('helloworld', 'o'),
    2. strpos('helloworld', 'or'),
    3. strpos('helloworld', 'x');
    4. +-------+-------+-------+
    5. | _col0 | _col1 | _col2 |
    6. +-------+-------+-------+
    7. | 5 | 7 | 0 |

SUBSTR/SUBSTRING

  1. substring(string str, bigint pos)
  2. substring(string str FROM pos)
  3. substring(string str, bigint pos, bigint len)
  4. substring(string str FROM pos FOR len)
  • 命令说明:

    • substring(varchar str, bigint pos)SUBSTRING(varchar str FROM pos)返回从pos位置开始到字符串结束的子串。如果pos<0,则起始位置从字符串的末尾开始倒数。

    • substring(varchar str, bigint pos, bigint len)SUBSTRING(varchar str FROM pos FOR len)返回从pos位置开始长度为len的子串。 如果pos<0,则起始位置从字符串的末尾开始倒数。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select substr('helloworld', 6);
    2. +-------------------------+
    3. | substr('helloworld', 6) |
    4. +-------------------------+
    5. | world |
    1. select substr('helloworld' from 6);
    2. +-------------------------+
    3. | substr('helloworld', 6) |
    4. +-------------------------+
    5. | world |
    1. select substr('helloworld', 6, 3);
    2. +----------------------------+
    3. | substr('helloworld', 6, 3) |
    4. +----------------------------+
    5. | wor |
    1. select substr('helloworld' from 6 for 3);
    2. +----------------------------+
    3. | substr('helloworld', 6, 3) |
    4. +----------------------------+
    5. | wor |

UPPER/UCASE

  1. upper(string str)
  • 命令说明:将字符串str中的字母转换为大写。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select upper('Aliyun');
    2. +-----------------+
    3. | upper('Aliyun') |
    4. +-----------------+
    5. | ALIYUN |

NORMALIZE

  1. normalize(string str)
  • 命令说明:使用NFC规范化形式返回字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select normalize('Aliyun');
    2. +---------------------+
    3. | normalize('Aliyun') |
    4. +---------------------+
    5. | Aliyun |

TO_UTF8

  1. to_utf8(string str)
  • 命令说明:返回字符串的UTF-8编码格式。

  • 返回值类型:VARBINARY。

  • 示例:

    1. select to_utf8('Aliyun');
    2. +-------------------+
    3. | to_utf8('Aliyun') |
    4. +-------------------+
    5. | 41 6c 69 79 75 6e |

ASCII

  1. ascii(string str)
  • 命令说明:返回字符str或者字符串str最左边字符对应的十进制ASCII值。

  • 返回值类型:BIGINT。

  • 示例:

    1. select ascii('2');
    2. +------------+
    3. | ascii('2') |
    4. +------------+
    5. | 50 |
    1. select ascii('dx');
    2. +-------------+
    3. | ascii('dx') |
    4. +-------------+
    5. | 100 |

BIN

  1. bin(bigint N)
  • 命令说明:返回N的二进制字符串。

    如果Nnull,则返回结果为null

  • 返回值类型:VARCHAR。

  • 示例:

    1. select bin(12);
    2. +---------+
    3. | bin(12) |
    4. +---------+
    5. | 1100 |

CHAR

  1. char(bigint N1, bigint N2...)
  • 命令说明:返回整数N1N2…对应的十进制ASCII码组成的字符串。

  • 返回值类型:VARBINARY。

  • 示例:

    1. select char(97,110,97,108,121,116,105,99,100,98);
    2. +----------------------------------------------------+
    3. | char(97, 110, 97, 108, 121, 116, 105, 99, 100, 98) |
    4. +----------------------------------------------------+
    5. | analyticdb |

CHAR_LENGTH/CHARACTER_LENGTH

  1. char_length(string str)
  • 命令说明: 以字符为单位返回字符串str的长度。

    一个汉字所对应的字符长度是1

  • 返回值类型:BIGINT。

  • 示例:

    1. select char_length('中国');
    2. +---------------------+
    3. | char_length('中国') |
    4. +---------------------+
    5. | 2 |
    1. select char_length('abc');
    2. +--------------------+
    3. | char_length('abc') |
    4. +--------------------+
    5. | 3 |

EXPORT_SET

  1. export_set(bigint bits, string on, string off[, string separator[, bigint number_of_bits]])
  • 命令说明:返回一个字符串,根据整数bits的二进制位01值,从右到左(从低位到高位)放置onoff字符串。1的位置放on字符串,0的位置放off字符串,由分隔符字符串(默认为逗号字符)分隔。 检查位数由number_of_bits指定,如果未指定,默认值为64

    如果检查位数大于64number_of_bits将被静默剪裁为64

    检查位数为-164,返回结果相同。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select export_set(5,'1','0',',',2);
    2. +---------------------------------+
    3. | export_set(5, '1', '0', ',', 2) |
    4. +---------------------------------+
    5. | 1,0 |
    1. select export_set(5,'1','0',',',10);
    2. +----------------------------------+
    3. | export_set(5, '1', '0', ',', 10) |
    4. +----------------------------------+
    5. | 1,0,1,0,0,0,0,0,0,0 |

FIND_IN_SET

  1. find_in_set(string str, string strlist)
  • 命令说明:返回str在列表strlist中的位置。

    如果str不在strlist中或者strlist是空字符串,返回结果为0

    如果strstrlist任一参数为null,返回结果为null

  • 返回值类型:BIGINT。

  • 示例:

    1. select find_in_set('b','a,b,c,d');
    2. +-----------------------------+
    3. | find_in_set('b', 'a,b,c,d') |
    4. +-----------------------------+
    5. | 2 |

FORMAT

  1. format(double X, bigint D)
  • 命令说明:将数字X格式化为#,###,###.##样式,舍入到D小数位,并将结果作为字符串返回。

    如果D0,则返回结果没有小数点或小数部分。

  • 返回值类型:BIGINT。

  • 示例:

    1. select format(12332.123456, 4)as result1, format(12332.1,4)as result2, format(12332.2,0)as result3;
    2. +-------------+-------------+---------+
    3. | result1 | result2 | result3 |
    4. +-------------+-------------+---------+
    5. | 12,332.1235 | 12,332.1000 | 12,332 |

FROM_BASE64

  1. from_base64(string str);
  • 命令说明:解码Base64编码过的字符串,并以BINARY类型输出。

  • 返回值类型:BINARY。

  • 示例:

    1. select from_base64(TO_BASE64('abc'));
    2. +-------------------------------------+
    3. | from_base64(TO_BASE64('abc')) |
    4. +-------------------------------------+
    5. | 61 62 63 |

HEX

  1. hex(bigint N)
  2. hex(string str)
  • 命令说明:返回整数N所对应的十六进制字符串,或者返回str中每个字符对应的十六进制数所组成的字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select hex(16);
    2. +---------+
    3. | hex(16) |
    4. +---------+
    5. | 10 |
    1. select hex('16');
    2. +-----------+
    3. | hex('16') |
    4. +-----------+
    5. | 3136 |

PRESTO_INSERT

  1. presto_insetr(string str,bigint pos,bigint len,string newstr);
  • 命令说明:将str中从pos位置开始、长度为len的字符串替换为newstr

  • 返回值类型:VARCHAR。

  • 示例:

    1. select presto_insert('Quadratic',3,4,'What');
    2. +---------------------------------------+
    3. | presto_insert('Quadratic',3,4,'What') |
    4. +---------------------------------------+
    5. | QuWhattic |

INSTR

  1. instr(string str, string substr)
  • 命令说明:返回字符串str中子字符串substr首次出现的位置。

  • 返回值类型:BIGINT。

  • 示例:

    1. select instr('foobarbar', 'bar');
    2. +---------------------------+
    3. | instr('foobarbar', 'bar') |
    4. +---------------------------+
    5. | 4 |

LOCATE

  1. locate(string substr, string str)
  2. locate(string substr, string str, bigint pos)
  • 命令说明:返回字符串str中首次出现substr的位置信息,或者返回字符串str中从指定位置pos开始首次出现substr的位置信息。

    如果substr不在str中,返回结果为0

    如果substr或者strnull,返回结果为null

  • 返回值类型:BIGINT。

  • 示例:

    1. select locate('bar', 'foobarbar');
    2. +----------------------------+
    3. | locate('bar', 'foobarbar') |
    4. +----------------------------+
    5. | 4 |
    1. select locate('bar', 'foobarbar', 7);
    2. +-------------------------------+
    3. | locate('bar', 'foobarbar', 7) |
    4. +-------------------------------+
    5. | 7 |

MID

  1. mid(string str, bigint pos, bigint len)
  • 命令说明:与SUBSTRING功能相同,从字符串strpos开始返回len长度的子字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select mid('Quadratically',5,6);
    2. +----------------------------+
    3. | mid('Quadratically', 5, 6) |
    4. +----------------------------+
    5. | ratica |
    1. select mid('Sakila', -5, 3);
    2. +--------------------------------+
    3. | mid('Sakila', INTEGER '-5', 3) |
    4. +--------------------------------+
    5. | aki |

OCT

  1. oct(bigint N)
  • 命令说明:返回整数N的八进制字符串表示形式。

    如果Nnull,返回结果为null

  • 返回值类型:VARCHAR。

  • 示例:

    1. select oct(12);
    2. +---------+
    3. | oct(12) |
    4. +---------+
    5. | 14 |

ORD

  1. ord(string str);
  • 命令说明:返回参数最左侧字符的字符编码。

  • 返回值类型:BIGINT。

  • 示例:

    1. select ord('2');
    2. +----------+
    3. | ord('2') |
    4. +----------+
    5. | 50 |

REPEAT

  1. repeat(string str, bigint count)
  • 命令说明:返回由字符串str重复count次数组成的字符串。

    如果count<1,则返回空字符串。

    如果strcountnull,则返回null

  • 返回值类型:VARCHAR。

  • 示例:

    1. select repeat('a', 3);
    2. +----------------+
    3. | repeat('a', 3) |
    4. +----------------+
    5. | aaa |
    1. select repeat('abc', null);
    2. +---------------------+
    3. | repeat('abc', null) |
    4. +---------------------+
    5. | NULL |
    1. select repeat(null, 3);
    2. +-----------------+
    3. | repeat(null, 3) |
    4. +-----------------+
    5. | NULL |

SOUNDEX

  1. soundex(string str);
  • 命令说明:返回参数str的SOUNDEX字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select soundex('Hello');
    2. +------------------+
    3. | soundex('Hello') |
    4. +------------------+
    5. | H400 |

SPACE

  1. space(bigint N)
  • 命令说明:返回由指定数量空格组成的字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select concat("#", space(6), "#");
    2. +----------------------------+
    3. | concat('#', space(6), '#') |
    4. +----------------------------+
    5. | # # |

SUBSTRING_INDEX

  1. substring_index(string str, string delimiter, bigint count);
  • 命令说明:返回str中分隔符delimitercount次出现之前的子字符串。

    • 如果count>0,将返回最后一个分隔符左边的所有子字符串。

    • 如果count<0,将返回最后一个分隔符右边的所有子字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select substring_index('www.mysql.com', '.', 2);
    2. +------------------------------------------+
    3. | substring_index('www.mysql.com', '.', 2) |
    4. +------------------------------------------+
    5. | www.mysql |

INITCAP

  1. inicap(string str);
  • 命令说明:将字符串的首字母变为大写,其它字母均为小写。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select initcap('the soap');
    2. +---------------------+
    3. | initcap('the soap') |
    4. +---------------------+
    5. | The Soap |

REGEXP_COUNT

  1. regexp_count(source_char, pattern);
  • 命令说明:在源字符串source_char中搜索正则表达式pattern,并返回该表达式在字符串中出现的次数。如果未找到匹配项,将返回0。

  • 返回值类型:BIGINT。

  • 示例:

    1. select regexp_count('rat cat\nbat dog', '.at');
    2. +-----------------------------------------+
    3. | regexp_count('rat cat\nbat dog', '.at') |
    4. +-----------------------------------------+
    5. | 3 |

REGEXP_SUBSTR

  1. regexp_substr(source_char, pattern);
  2. regexp_substr(source_char, pattern, position);
  • 命令说明:在源字符串source_char中从position位置搜索符合正则表达式pattern的子字符串。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select regexp_substr('Hello world bye', '\\b[a-z]([a-z]*)', 1);
    2. +---------------------------------------------------------+
    3. | regexp_substr('Hello world bye', '\\b[a-z]([a-z]*)', 1) |
    4. +---------------------------------------------------------+
    5. | orld |

TRANSLATE

  1. translate(expr, from_string, to_string);
  • 命令说明:将exprfrom_string中的每个字符都被替换为to_string中的相应字符。

  • 返回值类型:VARCHAR。

  • 示例:

    1. select translate('acbd','ab','AB');
    2. +-----------------------------+
    3. | translate('acbd','ab','AB') |
    4. +-----------------------------+
    5. | AcBd |
  • 本页导读 (1)
文档反馈