天天看点

clickhouse字符串函数

长度以及大小写

SELECT
	--11
    length('hello world') as str_length, 
    -- 判断字符串是否为空,空为1,非空为0
    --0
    empty('hello world'),
    --1
    notEmpty('hello world'),
    --11
    lengthUTF8('hello world'), 
    --11
    char_length('hello world'), -- 同 lengthUTF8()
    --11
    character_length('hello world'), -- 同 lengthUTF8(),
    --字母全部小写(将字符串中的ASCII转换为小写。)
    lower('abcd123--'),
    --字母全部大写(将字符串中的ASCII转换为大写。)
    upper('abcd123--'),
    lowerUTF8('abcd123-/*\8asd-\\'), -- abcd123-/*8asd-\
    upperUTF8('abcd123--'), -- ABCD123--
    --检查字符串是否为有效的UTF-8编码,是则返回1,否则返回0。
    --clickhouse没有布尔函数
    isValidUTF8('abcd123--/*\*'); 
           

字符串拼接

SELECT concat('Hello',' ','World', '!');
+----------------------------------+
|concat('Hello', ' ', 'World', '!')|
+----------------------------------+
|Hello World!                      |
+----------------------------------+

           

字符串截取

编码和截取有关系

SELECT
    substring('abcdefg', 1, 3),-- abc
    substring('你好,世界', 1, 3),-- 你
    substringUTF8('你好,世界', 1, 3); -- 你好,
+--------------------------+------------------------+----------------------------+
|substring('abcdefg', 1, 3)|substring('你好,世界', 1, 3)|substringUTF8('你好,世界', 1, 3)|
+--------------------------+------------------------+----------------------------+
|abc                       |你                       |你好,                         |
+--------------------------+------------------------+----------------------------+

           

删除空格

-- trimLeft(s) 返回一个字符串,用于删除左侧的空白字符
-- trimRight(s) 返回一个字符串,用于删除右侧的空白字符
-- trimBoth(s) 返回一个字符串,用于删除左侧和右侧的空白字符
SELECT
    trimLeft(' sdfdgs'), -- sdfdgs
    trimRight('abcd '), -- abcd
    trimBoth(' abcd '); -- abcd
           

判断

-- endsWith(s, suffix) 返回是否以指定的后缀结尾。如果字符串以指定的后缀结束,则返回1,否则返回0
-- startWith(s, prefix) 返回是否以指定的前缀开头。如果字符串以指定的前缀开头,则返回1,否则返回0。
SELECT
    endsWith('string','g'),
    startsWith('string', 'str'); -- 1 true
+-----------------------+---------------------------+
|endsWith('string', 'g')|startsWith('string', 'str')|
+-----------------------+---------------------------+
|1                      |1                          |
+-----------------------+---------------------------+