函数"/>
prestohive时间处理函数
presto and hive 常见时间处理函数(笔记)
—presto—
--presto+时间格式转化
A.时间格式转换:1.将timestamp时间格式 转换成字符串的格式(format_datetime处理对象是timestamp)
timestamp —— varchar
format_datetime(created_time,'yyyyMMdd')
format_datetime(created_time,'yyyy-MM-dd')2.将时间是字符串的'yyyyMMdd'格式 转化为 timestamp格式(date_format处理对象是date)
varchar —— timestamp
cast(concat(substring(dt, 1,4), '-', substring(dt, 5,2), '-', substring(dt,7,2)) as timestamp))
varchar —— date—— timestamp
cast(date_format(date_parse(dt,'%Y%m%d'),'%Y-%m-%d')as timestamp) 3.将字符串格式的时间'yyyy-MM-dd'转换成timestamp 格式
varchar —— timestamp
select cast(dt as timestamp) as create_time4.将时间格式2019-01-22 08:08:08 to unix时间格式
varchar——timestamp——unixtimestamp
to_unixtime(cast(‘2019-01-22 08:08:08’ as timestamp)) 5.将unix时间格式 to 北京时间格式 timestampfrom_unixtime(1548144488) 2019-01-22 08:08:08.0006.format_datetime 规范时间的具体格式
varchar(yyyy-MM-dd HH:mm:ss)——timestamp——varcharselect format_datetime(cast('2019-01-22 00:00:00' as timestamp),'yyyy-MM-dd')
select format_datetime(cast('2019-01-22 00:00:00' as timestamp),'yyyy-MM') B.比较常用的时间函数:前提是timestamp类型,所以你的表示时间的字段类型必须得转换为timestamp如果是timestamp类型了,可以直接用接下来得函数如果不是timestamp类型,要先转换成timestamp,参考A中情况对号入座1.date(dt) 返回日期表达式
timestamp——date
date(dt) 2.current_date 获取当前日期时间
——timestamp
current_date #返回:2019-12-11(timestamp类型)3.获取两个时间差的天数
timestamp——int
date_diff('day',cast('2018-09-05' as timestamp),cast('2018-09-07' as timestamp)) date_diff('day' or 'hour' or 'second',dt1,dt2) 'day','hour','second'表示天数、小时数、秒数4.dt1 = dt2+interval '1'day 观察dt2后一天的数据
date ——datedt1 between dt2 and dt2+interval 'n' day 观察dt之后的n天内的数据date(cast(concat(substring(vsl.dt, 1,4), '-', substring(vsl.dt, 5,2), '-', substring(vsl.dt,7,2)) as timestamp)) between date(a.time) and date(a.time)+interval '3' day5.获取昨日日期
format_datetime(date_add(‘day’,-1,current_date),‘yyyy-MM-dd’)
—hive-----
1.unixtimestamp和string转化
unixtimetamp10)——string
from_unixtime(1234567890,'yyyyMMdd')2.string和unixtimestamp转化
string date —— unixtimestamp(10)
unix_timestamp('2011-12-07 13:01:03')
unix_timestamp(string date, string pattern)
/*unix_timestamp(’20111207 13:01:03′,’yyyyMMdd HH:mm:ss’)*/3.string和date转化
string timestamp —— date
to_date(string timestamp)
/*to_date(’2011-12-08 10:03:01′)*/4.取日期一部分
year(string date)——int month (string date)day (string date)hour (string date)minute (string date)second (string date)weekofyear (string date) --当前日期在当前的周数
/*year(’2011-12-08′)
month(’2011-08-08′)
day(’2011-12-24′)
hour(’2011-12-08 10:03:01′)
minute(’2011-12-08 10:03:01′)
second(’2011-12-08 10:03:01′)
weekofyear(’2011-12-08 10:03:01′)
*/5.日期运算
datediff(string enddate, string startdate)——int
date_add(string startdate, int days)——string
date_sub (string startdate, int days)——string 6. 获取昨天、明天日期 date_format(date_add(current_date,-1),‘yyyy-MM-dd’)date_format(date_add(current_date,1),'yyyy-MM-dd')
更多推荐
prestohive时间处理函数
发布评论