六、clickhouse字符串函数
-- 1.字符串长度
SELECT length('hello world') as str_length, -- 按照Unicode编码计算长度“你好”的长度为6 empty('hello world'),-- 判断字符串是否为空,空为1,非空为0 notEmpty('hello world'), lengthUTF8('hello world'), -- 按照实际字符计算长度“你好”为2 char_length('hello world'), -- 同 lengthUTF8() character_length('hello world'), -- 同 lengthUTF8(), lower('abcd123--'),--字母全部小写(将字符串中的ASCII转换为小写。) upper('abcd123--'),--字母全部大写(将字符串中的ASCII转换为大写。) lowerUTF8('abcd123-/*\8asd-\\'), -- abcd123-/*8asd-\ upperUTF8('abcd123--'), -- ABCD123-- isValidUTF8('abcd123--/*\*'); --检查字符串是否为有效的UTF-8编码,是则返回1,否则返回0。 SELECT notEmpty(''), notEmpty(NULL), notEmpty('he'); -- 0,空,1 SELECT toValidUTF8('\x61\xF0\x80\x80\x80b'); -- reverseUTF8():以Unicode字符为单位反转UTF-8编码的字符串。如果字符串不是UTF-8编码,则可能获取到一个非预期的结果(不会抛出异常) SELECT reverse('abcdefg'), reverseUTF8('abcdefg');
-- 2.字符串维度自定义安排
SELECT format('{1} {0} {1}', 'World', 'Hello'); -- 输出:Hello World Hello SELECT format('{0} {0} {1} {1}', 'one', 'two'); -- 输出:one one two two SELECT format('{} {}', 'Hello', 'World'); -- 输出:Hello World
-- 3.字符串拼接 concat(s1,s2,s3,...)
SELECT concat('Hello',' ','World', '!');-- Hello World! -- 与concat相同,区别在于,你需要保证concat(s1, s2, s3) -> s4是单射的,它将用于GROUP BY的优化。 SELECT concatAssumeInjective('Hello',' ','World', '!');-- Hello World!
-- 4.字符串截取:substring(s, offset, length), mid(s, offset, length), substr(s, offset, length)
-- 以字节为单位截取指定位置字符串,返回以‘offset’位置为开头,长度为‘length’的子串。‘offset’从1开始(与标准SQL相同)。‘offset’和‘length’参数必须是常量。 SELECT substring('abcdefg', 1, 3),-- abc substring('你好,世界', 1, 3),-- 你 substringUTF8('你好,世界', 1, 3); -- 你好,
-- 5.字符串拼接:appendTrailingCharIfAbsent(s, c)
-- 如果‘s’字符串非空并且末尾不包含‘c’字符,则将‘c’字符附加到末尾。 SELECT appendTrailingCharIfAbsent('good','c'), -- goodc appendTrailingCharIfAbsent('goodccc','c'); -- goodccc
-- 6.字符串编码转换:convertCharset(s, from, to) 返回从‘from’中的编码转换为‘to’中的编码的字符串‘s’。
SELECT convertCharset('hello', 'UTF8','Unicode'),-- ��h convertCharset('hello', 'Unicode', 'UTF8'),-- 桥汬� convertCharset('hello', 'Unicode', 'ASCII'),-- convertCharset('hello', 'ascii', 'ascii'),--hello convertCharset('hello', 'UTF8','UTF8');-- hello SELECT base64Encode('username+password'),-- dXNlcm5hbWUrcGFzc3dvcmQ= base64Decode('dXNlcm5hbWUrcGFzc3dvcmQ='), -- username+password -- 使用base64将字符串解码成原始字符串。但如果出现错误,将返回空字符串。 tryBase64Decode('dXNlcm5hbWUrcGFzc3dvcmQ=');
-- 7.判断字符串是否已什么结尾或结束,返回1:true,0:flase
-- endsWith(s, suffix) 返回是否以指定的后缀结尾。如果字符串以指定的后缀结束,则返回1,否则返回0 -- startWith(s, prefix) 返回是否以指定的前缀开头。如果字符串以指定的前缀开头,则返回1,否则返回0。 SELECT endsWith('string','g'), startsWith('string', 'str'); -- 1 true
-- 8.删除左侧空白字符
-- trimLeft(s) 返回一个字符串,用于删除左侧的空白字符 -- trimRight(s) 返回一个字符串,用于删除右侧的空白字符 -- trimBoth(s) 返回一个字符串,用于删除左侧和右侧的空白字符 SELECT trimLeft(' sdfdgs'), -- sdfdgs trimRight('abcd '), -- abcd trimBoth(' abcd '); -- abcd
--9.对字符串进行转义以及反转义
--encodeXMLComponent:对字符串进行转义,针对 <、&、>、"、' 五种符号 --decodeXMLComponent:对字符串进行反转义,针对 <、&、>、"、' 五种符号 SELECT encodeXMLComponent('<name>'); /* ┌─encodeXMLComponent('<name>')─┐ │ <name> │ └──────────────────────────────┘ */ SELECT decodeXMLComponent('<name>'); /* ┌─decodeXMLComponent('<name>')─┐ │ <name> │ └────────────────────────────────────┘ */
--10.bitSlice(s, offset, length)
--返回从“偏移”索引中的“长度”位长的位开始的子字符串。位索引从 1 开始
select bitSlice('98979777uu',3,6); SELECT bitSlice('98979777uu', 3, 6) Query id: 103cfdcd-3bdf-48f9-8ae1-c9d8e87238a1 ┌─bitSlice('98979777uu', 3, 6)─┐ │ ➠ │ └──────────────────────────────┘ 1 rows in set. Elapsed: 0.002 sec.
--11.leftPad/rightPad
--可用于对某些敏感信息进行脱敏处理
SELECT leftPad(substring(phone,-3,3), length( phone ), '*') from (select '13126966152' phone); SELECT leftPad(substring(phone, -3, 3), length(phone), '*') FROM ( SELECT '13126966152' AS phone ) Query id: 128f77db-7396-4731-a77d-8c4aafda21de ┌─leftPad(substring(phone, -3, 3), length(phone), '*')─┐ │ ********152 │ └──────────────────────────────────────────────────────┘ 1 rows in set. Elapsed: 0.004 sec.
--12.countSubstrings/countSubstringsCaseInsensitive
--计算某个字符串中包含特定字符的数量
select countSubstrings('com.foo.com.bar.com', 'com') ,countSubstringsCaseInsensitive('BaBaB', 'A'); SELECT countSubstrings('com.foo.com.bar.com', 'com'), countSubstringsCaseInsensitive('BaBaB', 'A') Query id: f57c2361-e39c-44df-a8b1-2dfc2d9a9c41 ┌─countSubstrings('com.foo.com.bar.com', 'com')─┬─countSubstringsCaseInsensitive('BaBaB', 'A')─┐ │ 3 │ 2 │ └───────────────────────────────────────────────┴──────────────────────────────────────────────┘ 1 rows in set. Elapsed: 0.002 sec.
--13.countMatches
--基于正则表达式统计匹配数
select countMatches('foo.com bar.com baz.com bam.com', '([^. ]+)\.([^. ]+)'); SELECT countMatches('foo.com bar.com baz.com bam.com', '([^. ]+)\\.([^. ]+)') Query id: 06686144-3b42-4a3f-a3c8-b797d4d64916 ┌─countMatches('foo.com bar.com baz.com bam.com', '([^. ]+)\\.([^. ]+)')─┐ │ 4 │ └────────────────────────────────────────────────────────────────────────┘ 1 rows in set. Elapsed: 0.003 sec.
分类:
clickhouse数据库
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器