本文总结一下 Hive SQL字符串替换 函数:

1. replace()

替换单个字符或子字符串,replace(original-string, search-string, replace-string),实例:

--将字符串中所有的 e 全部替换为 x
select
    'show me the code' as string1,
    replace('show me the code', 'e', 'x') as string2
--将字符串中的子字符串 code 删除
select
    'show me the code' as string1,
    replace('show me the code', 'code','') as string2

2. translate()

分别对应替换单个字符,translate(string input, string from, string to),实例:

--将字符串中的 s  m  t  c 分别替换为 a  b  c  d
select
    'show me the code' as string1,
    translate('show me the code', 'smtc','abcd') as string2
--将字符串中的 s  m  t  c 都删除
select
    'show me the code' as string1,
    translate('show me the code', 'smtc','') as string2

3. regexp_replace()

替换符合正则表达式的字符或子字符串,regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT),实例:

--将字符串中的数字及多余空格都删除
select
    'show me the 404 code' as string1,
    regexp_replace('show me the 404 code', '\\d+ ', '') as string2
--将字符串中的换行符删除
select
    address as address1,
    regexp_replace(address, '\\n', '') as address2
from customer_info


原创文章,欢迎转载,转载请注明出处并留下原文链接。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注