匹配一个空白字符,包括/n,/r,/f,/t,/v等
/w
匹配一个可以组成单词的字符,包括下划线,如[/w]匹配”$5.98“中的5,等于[a-zA-Z0-9]
/W
匹配一个不可以组成单词的字符,如[/W]匹配”5.98“中的$,等于[^a-zA-Z0-9]
select regexp_extract('x=a3&x=18abc&x=2&y=3&x=4','x=([0-9]+)([a-z]+)',2)
得到结果:abc
日期函数
datediff:返回结束日期减去开始日期的天数
datediff(string enddate, string startdate)
select datediff('2021-11-20','2021-11-22')
date_add:返回开始日期startdate增加days天后的日期
date_add(string startdate, int days)
select date_add('2021-11-20',3)
date_sub:返回开始日期startdate减少days天后的日期
date_sub (string startdate, int days)
select date_sub('2021-11-22',3)
trunc 函数的用法
函数/方法 | 返回数据 |
trunc(add_months(current_date(),-1),'MM') | 上月1号 |
trunc(current_date(),'MM') | 本月1号 |
trunc(add_months(current_date(),1),'MM') | 下月1号 |
trunc(current_date(),'YYYY') | 今年年初日期 |
时间相关函数处理案例
1. 取得当前日期时间:
--取得当前日期:
select current_date();
输出:2021-08-14
--取得当前日期时间:
select current_timestamp();
输出:2021-08-14 13:14:57
--hive取得当前时间戳:
select unix_timestamp();
输出:1628911641
2. 日期时间转日期函数,返回日期时间字段中的日期部分,
-- 说明:字符串必须为:yyyy-MM-dd格式。
select to_date('2021-08-14 13:34:12');
输出:2021-08-14
3. 时间戳到转时间格式
--说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式
select from_unixtime(1323308945,’yyyy-MM-dd’);
输出:2011-12-08
select from_unixtime(1323308945,’yyyyMMdd’);
输出:20111208
--取得当前时间,相当于select current_timestamp();
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');
输出:2021-08-14 03:14:57
4. 日期、时间戳、字符串类型格式化输出标准时间格式
select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
输出:2021-08-14 11:14:46
select date_format(current_date(),'yyyy-MM-dd');
输出:2021-08-14
select date_format('2021-08-14','yyyy-MM-dd HH:mm:ss');
输出:2021-08-14 00:00:00
5. 获取当前时间的unix时间戳和日期转UNIX时间戳函数
select unix_timestamp();
输出:1628906623
select unix_timestamp('2021-08-14 10:05:20');
输出:1628935520
6. utc时间转换:
select from_utc_timestamp(current_timestamp(),8);
输出:2021-08-14 11:10:27.762
select to_utc_timestamp(current_timestamp(),8);
输出:2021-08-14 11:10:56.085
7. 日期转unix时间戳
select to_unix_timestamp('2021-08-14 11:10:27','yyyy-MM-dd HH:dd:ss');
输出:1628593227