200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 【hive 日期函数 大全】Hive常用日期函数整理 史上最全

【hive 日期函数 大全】Hive常用日期函数整理 史上最全

时间:2022-06-06 13:29:45

相关推荐

【hive 日期函数 大全】Hive常用日期函数整理 史上最全

【hive 日期函数 大全】Hive常用日期函数整理注意:1) hive 没有 to_char函数 2) HIVE 日期函数只识别 年-月-日 不能识别 年-月 ,所以处理月份的时候需要特殊处理1)hive 字符创拼接:CONCAT(string A, string B…)SELECT CONCAT('','05','11');2) 字符截取select substr(add_months(from_unixtime((unix_timestamp('-09','yyyy-MM')),'yyyy-MM-dd'),-1),0,7); SELECT SUBSTR(from_unixtime((unix_timestamp('09','yyyyMM')),'yyyyMMdd'),0,6);SELECT SUBSTR(from_unixtime((unix_timestamp('09','yyyyMM')),'yyyy-MM-dd'),0,7);hive (felix)> SELECT from_unixtime(unix_timestamp(cast('09' as string),'yyyyMM'),'yyyy-MM-dd') ;-09-01Time taken: 0.06 seconds, Fetched: 1 row(s)hive (felix)> SELECT from_unixtime(unix_timestamp(cast('09' as string),'yyyyMM'),'yyyy-MM') ;-09select from_unixtime(unix_timestamp(add_months(from_unixtime((unix_timestamp('09','yyyyMM')),'yyyy-MM-dd'),-1),'yyyy-MM-dd'),'YYYYMM') time_list3) 时间区间获取4) 中英文日期转换hive (default)> select from_unixtime((unix_timestamp('09','yyyyMM')),'MMM-YY');OKSep-15Time taken: 0.06 seconds, Fetched: 1 row(s)hive (default)> SELECT from_unixtime(unix_timestamp('Sep-19','MMM-YY'),'yyyyMM');OK12---英文日期大写hive (default)> select upper(from_unixtime((unix_timestamp('09','yyyyMM')),'MMM-YY'));OKSEP-15Time taken: 0.062 seconds, Fetched: 1 row(s)1.unix_timestamp()返回当前时区的unix时间戳返回类型:biginthive (default)> SELECT UNIX_TIMESTAMP();2.from_unixtime(bigint unixtime[,string format])时间戳转日期函数返回类型:stringhive (default)> select from_unixtime(unix_timestamp(),'yyyyMMdd') ;06143.unix_timestamp(string date)返回指定日期格式的的时间戳返回类型:bigint注意:如果后面只有date参数,date的形式必须为'yyyy-MM-dd HH:mm:ss'的形式。hive (default)> select unix_timestamp('-05-01');NULLhive (default)> select unix_timestamp('-05-01 00:00:00');14647104004.unix_timestamp(string date,string pattern)返回指定日期格式的时间戳返回类型:biginthive (default)> select unix_timestamp('-05-01','yyyy-MM-dd');1449331.to_date(string date)返回时间字段中的日期部分 必须是yyyy-MM-dd格式返回类型:string说明: 返回日期时间字段中的日期部分。只能识别到 “年-月-日” 级别的时间,无法识别 “年-月” 级别的时间。--1) 注意 使用年月,to_date 日期是识别不了的hive (default)> select to_date('-09'); --有问题,结果为nullOKNULL Time taken: 0.067 seconds, Fetched: 1 row(s)hive (default)> select add_months('-05-01',-1);OK-04-01hive (default)> select to_date('-05-01 00:00:00') ;-05-01hive (default)> select to_date('-05-01');-05-016.year(string date)返回时间字段中的年返回类型:inthive (default)> select year('-05-01 00:00:00') ;hive (default)> select year('-05-01') ;7.month(string date)返回时间字段中的月返回类型:inthive (default)> select month('-05-01') ;6hive (default)> select month('-05-01');68.day(string date)返回时间字段中的天返回类型:inthive (default)> select day('-05-01') ;19、day:返回日期中的天select day('-04-13 11:32:12');输出:1310、hour:返回日期中的小时select hour('-04-13 11:32:12');输出:1111、minute:返回日期中的分钟select minute('-04-13 11:32:12');输出:3212、second:返回日期中的秒select second('-04-13 11:32:56');输出:5613.weekofyear(string date)返回时间字段是本年的第多少周返回类型:inthive (default)> select weekofyear('-05-01') ;2214.datediff(string enddate,string begindate)返回enddate与begindate之间的时间差的天数返回类型:inthive (default)> select datediff('-05-01','-05-01') ;31--DEMO 2 hive (default)> SELECT datediff('-05-01 00:00:00','-05-01 23:00:00');OK3115.date_add(string date,int days)返回date增加days天后的日期返回类型:stringhive (default)> select date_add('-05-01',15) ;-05-1616.date_sub(string date,int days)返回date减少days天后的日期返回类型:stringhive (default)> select date_sub('-05-01',15) ;-05-1717:Hive中取最近30天数据datediff(CURRENT_TIMESTAMP ,gmt_create)<=30 18、Hive中 两个日期相差多少小时select (unix_timestamp('-05-25 12:03:55') - unix_timestamp('-05-25 11:03:55'))/3600输出:119、Hive中 两个日期相差多少分钟select (unix_timestamp('-05-25 12:03:55') - unix_timestamp('-05-25 11:03:55'))/60输出:6020、hive 计算某一个日期属于星期几,如-05-20 是星期日SELECT IF(pmod(datediff('-05-20', '1920-01-01') - 3, 7)='0', 7, pmod(datediff('-05-20', '1920-01-01') - 3, 7)) 输出:721、hive返回上个月第一天和最后一天--上个月第一天select trunc(add_months('-05-08',-1),'MM'); --只能识别 年-月-日 select trunc(add_months(CURRENT_TIMESTAMP,-1),'MM')hive (default)> select trunc(add_months('-05',-1),'MM'); OKNULLTime taken: 0.067 seconds, Fetched: 1 row(s)hive (default)> select trunc(add_months('-05-08',-1),'MM'); OK-04-01Time taken: 0.079 seconds, Fetched: 1 row(s)hive (default)> select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01'); --上个月最后一天select date_sub(trunc(CURRENT_TIMESTAMP,'MM'),1);--返回上个月日期 select substr(add_months(from_unixtime((unix_timestamp('-09','yyyy-MM')),'yyyy-MM-dd'),-1),0,7);SELECT substr(from_unixtime(unix_timestamp('-05','yyyy-MM'),'yyyyMM'),0,4);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。