Hive版本: hive-1.1.0-cdh5.14.2

1. Unix时间戳转日期:from_unixtime

语法:from_unixtime(bigint unixtime[, string format])
返回值:string
描述: Unix时间戳转换为日期格式

0: jdbc:hive2://node03:10000> select from_unixtime(1237573801, 'yyyy-MM-dd HH:mm:ss') as time_string;
+----------------------+--+
|     time_string      |
+----------------------+--+
| 2009-03-21 02:30:01  |
+----------------------+--+
1 row selected (0.14 seconds)
0: jdbc:hive2://node03:10000> select from_unixtime(1237573801) as time;
+----------------------+--+
|         time         |
+----------------------+--+
| 2009-03-21 02:30:01  |
+----------------------+--+
1 row selected (0.126 seconds)
2. 当前Unix时间戳:unix_timestamp

语法:unix_timestamp()
返回值: bigint
描述:返回当前时间的Unix时间戳

0: jdbc:hive2://node03:10000> select unix_timestamp() as current_time;
+---------------+--+
| current_time  |
+---------------+--+
| 1592639728    |
+---------------+--+
3. 日期转Unix时间戳:unix_timestamp

语法: unix_timestamp(string date)
返回值: bigint
描述:返回指定日期的Unix时间戳

0: jdbc:hive2://node03:10000> select unix_timestamp('2009-03-20 11:30:01') as time_in_seconds;
+------------------+--+
| time_in_seconds  |
+------------------+--+
| 1237519801       |
+------------------+--+
1 row selected (0.138 seconds)
4. 指定日期某部分转为Unix时间戳:unix_timestamp

语法:unix_timestamp(string date, string pattern)
返回值: bigint
描述:根据指定的pattern,转换日期为Unix时间戳

0: jdbc:hive2://node03:10000> select unix_timestamp('2009-03-20', 'yyyy-MM-dd') as time_in_seconds;
+------------------+--+
| time_in_seconds  |
+------------------+--+
| 1237478400       |
+------------------+--+
1 row selected (0.121 seconds)
5. 时间戳转日期:to_date

语法:to_date(string timestamp)
返回值: 2.1.0版本前返回string,2.1.0版本后返回date
描述: 把指定的时间戳转换为日期格式

0: jdbc:hive2://node03:10000> select to_date("1970-01-01 00:00:00") as date1;
+-------------+--+
|    date1    |
+-------------+--+
| 1970-01-01  |
+-------------+--+
1 row selected (0.141 seconds)
6. 提取日期中的年份:year

语法:year(string date)
返回值:int
描述:提取日期中的年份部分

0: jdbc:hive2://node03:10000> select year('2020-06-20 16:01:00') as year;
+-------+--+
| year  |
+-------+--+
| 2020  |
+-------+--+
1 row selected (0.116 seconds)
0: jdbc:hive2://node03:10000> select year('2020-06-20') as year;
+-------+--+
| year  |
+-------+--+
| 2020  |
+-------+--+
1 row selected (0.119 seconds)
7. 提取日期中的月份:month

语法:month(string date)
返回值: int
描述:提取日期中的月份部分

0: jdbc:hive2://node03:10000> select month('2020-06-20') as month;
+--------+--+
| month  |
+--------+--+
| 6      |
+--------+--+
1 row selected (0.127 seconds)
0: jdbc:hive2://node03:10000> select month('2020-06-20 16:08:00') as month;
+--------+--+
| month  |
+--------+--+
| 6      |
+--------+--+
1 row selected (0.112 seconds)
8. 提取日期中的日:day

语法: day(string date) 、dayofmonth(date)
返回值: int
描述:提取日期中的日部分

0: jdbc:hive2://node03:10000> select day('2020-06-20') as day;
+------+--+
| day  |
+------+--+
| 20   |
+------+--+
1 row selected (0.189 seconds)
0: jdbc:hive2://node03:10000> select dayofmonth('2020-06-20') as day;
+------+--+
| day  |
+------+--+
| 20   |
+------+--+
1 row selected (0.106 seconds)
9. 提取日期中的小时:hour

语法:hour(string date)
返回值:int
描述:提取日期中的小时部分

0: jdbc:hive2://node03:10000> select hour('2020-06-20 16:08:00') as hour;
+-------+--+
| hour  |
+-------+--+
| 16    |
+-------+--+
1 row selected (0.113 seconds)
0: jdbc:hive2://node03:10000> select hour('16:08:00') as hour;
+-------+--+
| hour  |
+-------+--+
| 16    |
+-------+--+
10. 提取日期中的分钟/秒:minute/second

语法:minute(string date) / second(string date)
返回值: int
描述:提取日期中的分钟/秒部分

0: jdbc:hive2://node03:10000> select hour('2020-06-20 16:08:00') as hour,
. . . . . . . . . . . . . . > minute('2020-06-20 16:08:00') as monute,
. . . . . . . . . . . . . . > second('2020-06-20 16:08:00') as second
. . . . . . . . . . . . . . > ;
+-------+---------+---------+--+
| hour  | monute  | second  |
+-------+---------+---------+--+
| 16    | 8       | 0       |
+-------+---------+---------+--+
11. 返回日期处于当年的第几周:weekofyear

语法:weekofyear(string date)
返回值:int
描述: 返回指定日期处于当年的第几周

0: jdbc:hive2://node03:10000> select weekofyear('2020-06-20') as weekofyear;
+-------------+--+
| weekofyear  |
+-------------+--+
| 25          |
+-------------+--+
1 row selected (0.139 seconds)
12. 两日期间隔天数:datediff

语法:datediff(string enddate, string startdate)
返回值: int
描述: 返回两指定日期之间的天数

0: jdbc:hive2://node03:10000> select datediff('2020-06-20', '2020-06-15') as days;
+-------+--+
| days  |
+-------+--+
| 5     |
+-------+--+
1 row selected (0.138 seconds)
13. 日期增加函数: dateadd

语法:date_add(date/timestamp/string startdate, tinyint/smallint/int days)
返回值: 2.1.0版本前返回string,2.1.0版本后返回date
描述:返回开始日期startdate增加天数days后的日期

0: jdbc:hive2://node03:10000> select date_add('2020-06-15', 5) as date;
+-------------+--+
|    date     |
+-------------+--+
| 2020-06-20  |
+-------------+--+
1 row selected (0.14 seconds)
14. 日期减少函数: datesub

语法: date_sub(date/timestamp/string startdate, tinyint/smallint/int days)
返回值: 2.1.0版本前返回string,2.1.0版本后返回date
描述: 返回开始日期startdate减少天数days后的日期

0: jdbc:hive2://node03:10000> select date_sub('2020-06-20', 5) as date;
+-------------+--+
|    date     |
+-------------+--+
| 2020-06-15  |
+-------------+--+
1 row selected (0.128 seconds)
15. 当前日期/当前时间戳:current_date / current_timestamp

语法: current_date / current_timestamp
返回值:date / timestamp
描述: 返回当前日期或者时间戳

0: jdbc:hive2://node03:10000> select current_date as date, current_timestamp as timestamp;
+-------------+--------------------------+--+
|    date     |        timestamp         |
+-------------+--------------------------+--+
| 2020-06-20  | 2020-06-20 18:19:35.736  |
+-------------+--------------------------+--+
1 row selected (0.145 seconds)
16. 月份增加函数:add_months

语法: add_months(string start_date, int num_months, output_date_format)
返回值: string
描述: 返回开始日期start_date增加num_months月后的日期,output_date_format做为可选参数只在Hive 4.0.0版本后可用。

0: jdbc:hive2://node03:10000> select add_months('2020-06-30', 1) as add_months;
+-------------+--+
| add_months  |
+-------------+--+
| 2020-07-31  |
+-------------+--+
1 row selected (0.138 seconds)
17. 当月最后一天:last_day

语法:last_day(string date)
返回值: string
描述: 返回指定日期所在月份的最后一天

0: jdbc:hive2://node03:10000> select last_day(current_date) as last_day;
+-------------+--+
|  last_day   |
+-------------+--+
| 2020-06-30  |
+-------------+--+
1 row selected (0.117 seconds)
0: jdbc:hive2://node03:10000> select last_day(current_timestamp) as last_day;
+-------------+--+
|  last_day   |
+-------------+--+
| 2020-06-30  |
+-------------+--+
1 row selected (0.105 seconds)
18. 下一个周几的日期:next_day

语法: next_day(string start_date, string day_of_week)
返回值: string
描述: 返回开始日期start_date后的第一个周几(如周一)的日期,其中day_of_week可以为星期的2或3位缩写如:Mo, tue,也可以是星期的全拼,如:FRIDAY

0: jdbc:hive2://node03:10000>  select next_day('2020-06-20', 'MON') as next_monday;
+--------------+--+
| next_monday  |
+--------------+--+
| 2020-06-22   |
+--------------+--+
1 row selected (0.16 seconds)
19. 截断日期:trunc

语法:trunc(string date, string format)
返回值: string
描述:按指定格式截断日期,format可以为月或年:MONTH/MON/MM, YEAR/YYYY/YY

0: jdbc:hive2://node03:10000> select trunc('2020-06-20', 'MM') as trunc_mon;
+-------------+--+
|  trunc_mon  |
+-------------+--+
| 2020-06-01  |
+-------------+--+
1 row selected (0.133 seconds)
0: jdbc:hive2://node03:10000> select trunc('2020-06-20', 'YEAR') as trunc_date;
+-------------+--+
| trunc_date  |
+-------------+--+
| 2020-01-01  |
+-------------+--+
1 row selected (0.164 seconds)
20. 计算间隔月份:months_between

语法:months_between(date1, date2)
返回值: double
描述: 返回两日期的间隔月份,精度到小数点后最多8位

0: jdbc:hive2://node03:10000> select months_between('2020-06-20','2020-06-01') as months_between1,
. . . . . . . . . . . . . . > months_between('2020-06-20','2020-06-20') as months_between2,
. . . . . . . . . . . . . . > months_between('2020-06-20','2020-05-20') as months_between3,
. . . . . . . . . . . . . . > months_between('2020-06-20','2020-07-20') as months_between4;
+------------------+------------------+------------------+------------------+--+
| months_between1  | months_between2  | months_between3  | months_between4  |
+------------------+------------------+------------------+------------------+--+
| 0.61290323       | 0.0              | 1.0              | -1.0             |
+------------------+------------------+------------------+------------------+--+
1 row selected (0.127 seconds)

【Hive】日期函数相关推荐

  1. 【hive 日期函数】Hive常用日期函数整理

    1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12'); 输出:2015-04-02 1 2 2.from_unixtime:转化unix时 ...

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

    [hive 日期函数 大全]Hive常用日期函数整理注意:1) hive 没有 to_char函数 2) HIVE 日期函数只识别 年-月-日 不能识别 年-月 ,所以处理月份的时候需要特殊处理1)h ...

  3. hive日期函数总结

    Hive 日期函数 Hive Date Functions 官网地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+U ...

  4. Hive日期函数使用

    Hive日期函数使用 year month day hour minute second add_months date_add date_sub date_format datediff dayof ...

  5. hive日期函数使用大全

    以下hive日期函数百分百满足日常工作使用,请仔细阅读!! [hive 日期函数 大全]Hive常用日期函数整理注意:1) hive 没有 to_char函数 2) HIVE 日期函数只识别 年-月- ...

  6. hive 日期函数_数据分析面试必备——SQL窗口函数你会了吗?

    之前写过一篇sql的文章,面向基础的sql操作(无眠:数据分析面试必备--SQL你准备好了吗?),目前已经有12000+收藏(收藏是点赞的5倍,你们可真狠心哪),也可以看出众多同学对sql学习的热情. ...

  7. hive日期函数转化

    1.to_date:日期时间转日期函数 select to_date('2015-04-02 13:34:12'); 输出:2015-04-02 1 2 2.from_unixtime:转化unix时 ...

  8. [转载]Hive日期函数

    转自大神 http://www.oratea.net/?p=944 无论做什么数据,都离不开日期函数的使用. 这里转载一下Hive的日期函数的使用,写的相当完整. 日期函数UNIX时间戳转日期函数: ...

  9. HIVE日期函数大全

    转自大神 http://www.oratea.net/?p=944 无论做什么数据,都离不开日期函数的使用. 这里转载一下Hive的日期函数的使用,写的相当完整. 日期函数UNIX时间戳转日期函数: ...

  10. hive日期函数处理

    1. 日期函数unix时间转日期函数:from_unixtime,语法为from_unixtime(bigint unixtime, stringformat) select from_unixtim ...

最新文章

  1. AAAI 2021 顶会论文开源,OCR方向最火开源项目已超1万 star!
  2. android中设置控件获得焦点
  3. c# asp.net core取当月第一天和最后一天及删除最后一个字符的多种方法
  4. 【PHP】关于IPv4、IPv6 的操作函数
  5. java判断括号是否闭合_用 java 判断 括号是否完全匹配
  6. ctrllist如何多行显示_浩辰CAD教程:如何输入钢筋符号?
  7. Angular2 依赖注入
  8. Linux环境下虚拟化之KVM常用命令
  9. Postman工具(环境变量与全局变量)
  10. 第四季-专题7-Linux内核链表
  11. windows oracle补丁包,Windows下oracle打补丁步骤
  12. Oracle DBA遇到频次最高的五十多个问题
  13. 单核性能强悍,Core i3 这次又要“默秒全”?
  14. 微信公众平台:微信网页授权和微信支付
  15. 计算机中丢失msvcr100.dll怎么办,Win7计算机中Msvcr100.dll丢失的解决方法
  16. 凸优化理论基础1--仿射集
  17. RPA之家直播公开课
  18. 【评测】照胶的仪器选购
  19. Linux系统重装出现c0409a9f,自学IT吧论坛Linux系统运营系列视频教程#28期2016系统/服务器资源天地 - www.zxit8.com...
  20. css好看常用的中文字体

热门文章

  1. leetcodepython_LeetCode 答案(python)1-17
  2. android硬件加速器及其问题小结
  3. Cocos2dx---之粒子系统
  4. Windows 8 Directx 开发学习笔记(十)纹理贴图实现旋转的木箱
  5. 浅谈UWB室内定位(二)
  6. samba配置不同用户不同权限_前端问题集:vue配置环境-给不同的环境配不同的打包命令...
  7. docker 私服搭建以及镜像部署
  8. java获取当前日期和时间的二种方法分享
  9. E72上安装fring使用skypeout拨打电话
  10. HelloDjango 第 10 篇:小细节 Markdown 文章自动生成目录,提升阅读体验