1. 长度计算 length
1 | -- 按照实际长度计算 |
2. isValidUTF8 检测 UTF8 编码
- 检查字符串是否为有效的UTF-8编码,是则返回1,否则返回0。
1 | SELECT isValidUTF8('hello world'),isValidUTF8('你好'); |
3. 为空判断 empty
判断字符串是否为空,空为1 ,不为空为0
1 | select empty('hello world'),empty(''); |
4. 非空判断 notEmpty
判断字符串是否不为空,不为空 1,为空 0
1 | select notEmpty('hello world'),notEmpty(''),notEmpty(NULL); |
5. 转小写 lower
字母全部小写
1 | SELECT lower('hello WORLD'),lowerUTF8('hello WORLD'); |
6. 转大写 upperUTF8
1 | SELECT upper('hello WORLD'),upperUTF8('hello WORLD'); |
7. 字符反转 reverse
1 | SELECT reverse('hello world'), reverseUTF8('hello world'); |
8. 字符串定义 format
1 | SELECT format('{1} {0} {1}', 'World', 'Hello'); |
9. 字符串拼接 concat
1 | SELECT concat('Hello',' ','World', '!'); |
10. 字符串拼接 concatAssumeInjective
- 与concat相同,
- 区别在于,需要保证concat(s1, s2, s3) -> s4是单射的,它将用于GROUP BY 的优化。
1 | SELECT concatAssumeInjective('Hello',' ','World', '!'); |
11. 字符串拼接 appendTrailingCharIfAbsent
appendTrailingCharIfAbsent(str, c)
如果 ‘ str ‘ 字符串非空并且末尾不包含 ‘ c ‘ 字符,则将 ‘ c ‘ 字符附加到末尾。
1 | SELECT appendTrailingCharIfAbsent('hello','o'),appendTrailingCharIfAbsent('hello','y'); |
12. 数组合并成字符串 arrayStringConcat
1 | SELECT arrayStringConcat(['one','two','three']); |
13. 字符串截取 substring
- 截取指定位置字符串,
- 返回以 ‘ offset ‘ 位置为开头,长度为 ‘ length ‘ 的子串
- ‘ offset ‘ 从1开始(与标准SQL相同)
- ‘ offset ‘ 和 ‘ length ‘ 参数必须是常量。
1 | SELECT substring('abcdefg', 1, 3),substring('你好世界', 1, 3); |
14. 字符串截取 mid
1 | SELECT mid('abcdefg', 1, 3),mid('你好世界', 1, 3); |
15. 字符串截取 substr
1 | SELECT substr('abcdefg', 1, 3),substr('你好世界', 1, 3); |
16. 使用正则获取字符串 extract
- extract(haystack, pattern)
- 使用正则表达式截取字符串。
- 如果 ‘ haystack ‘ 与 ‘ pattern ‘ 不匹配,则返回空字符串。
1 | SELECT extract('HELLO2 world3', '[0-9]'),extract('HELLO world', '[0-9]'); |
17. 使用正则获取字符串 extractAll
- extract(haystack, pattern)
- 同extract,获取匹配的全部字符串
1 | SELECT extractAll('HELLO2 world3', '[0-9]'),extractAll('HELLO world', '[0-9]'); |
18. 字符串编码转换 convertCharset
- convertCharset(s, from, to)
- 返回从 ‘ from ‘ 中的编码转换为 ‘ to ‘ 中的编码的字符串 ‘ s ‘ 。
1 | SELECT convertCharset('hello', 'UTF8','Unicode'); |
19. 字符串转 base64 编码 base64Encode
1 | SELECT base64Encode('hello world'); |
20. base64 编码 转 字符串 base64Decode
- 使用base64将字符串解码成原始字符串
- 如果转换失败将抛出异常
1 | select base64Decode('aGVsbG8gd29ybGQ='); |
base64 编码 转 字符串 tryBase64Decode
- 使用base64将字符串解码成原始字符串。
- 但如果出现错误,将返回空字符串。
1 | select tryBase64Decode('aGVsbG8gd29ybGQ='); |
21. 判断指定字符开头 startsWith
- startWith(s, prefix)
- 返回是否以指定的前缀开头。
- 如果字符串以指定的前缀开头,则返回1,否则返回0。
1 | select startsWith('hello world','h'),startsWith('hello world','s'); |
22. 判断指定字符结尾 endsWith
- endsWith(s, suffix)
- 返回是否以指定的后缀结尾。
- 如果字符串以指定的后缀结束,则返回1,否则返回0
1 | SELECT endsWith('hello world', 'd'),endsWith('hello world', 's'); |
23. 删除左侧空字符 trimLeft
- trimLeft(s)
- 返回一个字符串,用于删除左侧的空白字符
1 |
|
24. 删除右侧空字符 trimRight
- trimRight(s)
- 返回一个字符串,用于删除右侧的空白字符
1 | select trimRight(' a b c ') as str , endsWith(str,'c'); |
25. 删除两侧空字符 trimRight
- trimBoth(s)
- 返回一个字符串,用于删除左侧和右侧的空白字符
1 | select trimBoth(' a b c ') as str ,startsWith(str,'a'), endsWith(str,'c'); |
26. 字符串拆分 splitByChar
- splitByChar(separator, string)
- ‘ separator ‘ 必须为仅包含一个字符的字符串常量。
- ‘ string ‘ 要被拆分的字符串
- 返回拆分后的子串的数组
1 | -- 如果分隔符出现在字符串的开头或结尾,或者如果有多个连续的分隔符,则将在对应位置填充空的子串。 |
27. 字符串拆分 splitByString
- splitByString(separator, string)
- 用法与splitByChar相同
- 它使用多个字符的字符串作为分隔符。
- 该字符串必须为非空
1 | select splitByString('l','hello world'); |
28. 拆分连续字符 alphaTokens
- alphaTokens(s)
- 从范围a-z和A-Z中选择连续字节的子字符串
- 返回子字符串数组
1 | SELECT alphaTokens('ab1cd2ef3gh'); |
29. 字符串替换 replace
- replace(str, pattern, replacement)
- 在 ‘ str ‘ 字符串中匹配所有的 ‘ pattern ‘ 字符替代成 ‘ replacement ‘ 字符
1 | select replace('hello world', 'l', 'L') AS replace; |
30. 字符串替换 replaceAll
- 同 replace
1 | select replaceAll('hello world', 'l', 'L') AS replace; |
字符串替换 replaceOne
- replaceOne(haystack, pattern, replacement)
- 替换匹配到的字符串
- 用 ‘ replacement ‘ 子串替换 ‘ haystack ‘ 中与 ‘ pattern ‘ 子串第一个匹配的匹配项
- ‘pattern’和‘replacement’必须是常量。
1 |
|
31. 正则替换 replaceRegexpOne
1 | -- 赋值字符串10次 |
32. 正则替换 replaceRegexpAll
1 | -- 与replaceRegexpOne相同,但会替换所有出现的匹配项。例如: |
33. 搜索字符串出现位置 pasition
- pasition(str1, str2)
- 显示 str2 在 str1 的第一个出现的位置
1 | SELECT POSITION('hello world', 'e'); |
34. 搜索字符串出现位置 positionUTF8
1 | select positionUTF8('你好','好') AS positionUTF8; |
35. 不区分大小写搜索字符串出现位置 positionCaseInsensitive
1 | select positionCaseInsensitive('hello world','ll') AS positionCaseInsensitive; |
36. 定位 locate
1 | select locate('hello world','ll'); |
37. 模糊匹配 like
- like()
- 注意大写敏感。
- ‘ % ‘表示任何字节数(包括零字符)
- ‘ _ ‘表示任何一个字节
1 | select like('hello world', '%lo%'),like('hello', '_ell_'); |
38. 模糊匹配 not like
1 | select notLike('hello world', '%lo%'),notLike('hello', '_ell_'); |