concat():
函数用于将多个字符串连接成一个字符串。
语法:concat (str1,str2,…) ;
返回值:string;
特点:如有任何一个参数为null ,则返回值为 null。
举例:
-- 案例1:将test1中的字段拼接成一个字符串——》test1中的字段名abc下的数据也为abc,下同
hive> select concat(abc, def, gh) from test1;
[end]: abcdefgh
-- 案例2:将test1中的字段拼接成字符串,以“:”分隔
hive> select concat(abc, ':', def, ':', gh) from test1;
[end]: abc:def:gh
-- 案例3:将test1中abc字段拼接null值
hive> select concat(abc, null) as item1 from test1;
[end]: null
concat_ws():
可以指定参数之间的分割符,并返回字符串。concat_ws() 代表 concat With Separator ,是concat()的特殊形式。分隔符放置在要连接的每个字符串之间。
语法:concat_ws(string SEP, string1, string2… );
返回值:string;
特点:分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 null,则结果为 null,拼接的字符串为null,则会忽略该字符串。
举例:
-- 案例1:将test1中的字段拼接成字符串,以“_”分隔
hive> selece concat_ws('_', abc, def, gh) from test1;
[end]: abc_def_gh
-- 案例2:将test1中的字段拼接上null值,以“_”分隔
hive> selece concat_ws('_', abc, def, null) from test1;
[end]: abc_def