Hive SQL 基础语法(1)

1 substr , substring

字符串截取函数:substr,substring

语法: substr(string A, int start, int len),substring(string A, int start, int len)

返回值: string

说明:返回字符串 A 从 start 位置开始,长度为 len 的字符串

举例:

hive> select substr(‘abcde’,3,2) from dual;  
cd  
hive> select substring(‘abcde’,3,2) from dual;  
cd  
hive>select substring(‘abcde’,-2,2) from dual;  
de

2 to_date

日期时间转日期函数: to_date

语法: to_date(string timestamp)

返回值: string

说明: 返回日期时间字段中的日期部分。

举例:

hive>   select to_date('2011-12-08 10:03:01') from dual;
2011-12-08

3 concat_ws

带分隔符字符串连接函数:concat_ws

语法: concat_ws(string SEP, string A, string B…)

返回值: string

说明:返回输入字符串连接后的结果,SEP 表示各个字符串间的分隔符

举例:

hive> select concat_ws(‘,’,'abc’,'def’,'gh’) from dual;  
abc,def,gh  

4 datediff

日期比较函数: datediff

语法: datediff(string enddate, string startdate)

返回值: int

说明: 返回结束日期减去开始日期的天数。

举例:

hive>   select datediff(’2012-12-08′,’2012-05-09′) from dual;
213

5 pmod

正取余函数 : pmod

语法: pmod(int a, int b),pmod(double a, double b)

返回值: int double

说明: 返回正的 a 除以 b 的余数

hive> select pmod(9,4) from lxw_dual;
1
hive> select pmod(-9,4) from lxw_dual;
3

6 datediff 配合 pmod

两个函数配合可以计算当前日期对应为星期几

pmod(datediff(#date#, '1920-01-01') - 3, 7)

7 weekofyear

日期转周函数: weekofyear

语法: weekofyear (string date)

返回值: int

说明: 返回日期在当前的周数。

举例:

hive> select weekofyear('2011-12-08 10:03:01') from lxw_dual;
49

8 concat

concat() 函数可以连接一个或者多个字符串

CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。

select concat('11','22','33'); 
112233

9 IF(Test Condition, True Value, False Value)

The IF condition evaluates the “Test Condition” and if the “Test Condition” is true, then it returns the “True Value”. Otherwise, it returns the False Value.

Example:

IF(1=1, 'working', 'not working') returns 'working'

10 IN (val1, val2, …)

A IN (val1, val2, …)
boolean

TRUE if A is equal to any of the values.

IN 操作符允许我们在 WHERE 子句中规定多个值。

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)

11 COALESCE

非空查找函数: COALESCE

语法: COALESCE(T v1, T v2,…)

返回值: T

说明: 返回参数中的第一个非空值;如果所有值都为NULL,那么返回NULL

举例:

hive> select COALESCE(null,'100','50′) from lxw_dual;

100

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值