相关文章推荐
严肃的荒野  ·  浙江工商大学·  1 年前    · 
卖萌的滑板  ·  地下偶像第十四集原版-西瓜视频搜索·  1 年前    · 
飘逸的哑铃  ·  “素媛案”罪犯即将出狱,称想见受害者,多次表 ...·  2 年前    · 
鼻子大的酱牛肉  ·  叶罗丽:新仙子古风造型像罗丽,金王子打造了曼 ...·  2 年前    · 
huangendabbs  ·  人民日报探析资本主义系统性危机:根源、如何看 ...·  4 年前    · 
Code  ›  hive时间和字符串互转,时间函数-天翼云
https://www.ctyun.cn/zhishi/p-262709
阳刚的蚂蚁
1 年前

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
查看全部活动
热门活动
  • 2024云享优惠·数智升级 NEW 爆款2核4G3M云服务器82.8元/年起
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 一键部署Llama3大模型学习机 0代码一键部署,预装最新主流大模型Llama3与StableDiffusion
  • 中小企业上云场景组合购 NEW 建站、域名等场景一站式购齐,组合购9元起!
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 云主机特惠 HOT 多款云主机新老同享,满足您的上云需求!
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城 进入云市场
办公协同
  • WPS云文档 EMM手机管家 智能商业平台 SSL证书 新域名服务 翼电子教室 翼智慧显示一体化解决方案 税务风控云 翼云智眼安消一体化解决方案 翼信息化运维服务 翼微信营销解决方案 翼视频云归档解决方案 云管家2.0 智慧工厂_生产流程管理解决方案 全栈混合云敏捷版(软件) 全栈混合云敏捷版(一体机)

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云技术合作伙伴
  • 天翼云OpenAPI中心 天翼云EasyCoding平台
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云培训认证
  • 天翼云学堂 天翼云市场商学院
天翼云服务合作伙伴
  • 天翼云MSP合作伙伴 天翼云集成商交付能力认证
天翼云合作计划
  • 央企云合作计划
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴 天翼云甄选商城合作伙伴
天翼云东升计划
  • 东升适配创新中心 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
资源与工具
  • 天翼云OpenAPI EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂 天翼云认证

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • OpenAPI中心 客户支持计划 专家技术服务

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云 天翼云4.0 天翼云APP 全球基础设施 分析师和白皮书 天翼云·创新直播间 2024智算云生态大会 2023云生态大会 2022云生态大会 天翼云中国行 支持与服务 了解天翼云
  • 站点切换
    • 中国站
  • 文档
  • 控制中心
  • 备案
  • 管理中心

hive时间和字符串互转,时间函数

首页 知识中心 数据库 文章详情页

hive时间和字符串互转,时间函数

2023-08-01 08:54:03 阅读次数:109

hive, 日期

hive里经常需要将字符串转化为date或timestamp 或者转化为日期格式的string

先说一个简单的 cast(xx as date/string/timestamp) 这个大多情况都可以用

1.to_date

to_date只保留年月日,参数必须是string类型的yyyy-MM-dd HH:mm:ss或者date或timestamp类型, 返回值是date类型,注意这个返回类型,这个是少数返回date类型的函数

---2023-03-21----补充下

这个函数还有一个特别好用的用法

select to_date('2022-1-2') ->也是可以转化为date的。 由此引申

select to_date(replace( '2022/1/2','/','-' )) 得到标准的date 由此再引申

select date_format(to_date(replace( '2022/1/2','/','-' )),'yyyy-MM-dd') 转化非标准时间为标准

select "to_date('2009-07-30 04:17:52')",to_date('2009-07-30 04:17:52') union all
select "to_date('2009-07-30 04')",to_date('2009-07-30 04') union all
select "to_date(current_date)",to_date(current_date) union all
select "to_date(current_timestamp)",to_date(current_timestamp)

2.date_format

date_format(date/timestamp/string, fmt) - converts a date/timestamp/string to a value of string in the format specified by the date format fmt.

date_format 参数1可以是date timestamp 和string类型,第二个是format格式(yyyy-MM-dd hh:mm:ss), 返回值是string 。

select "date_format('2009-07-30 12:13:14','yyyyMMddHHmmss')", date_format('2009-07-30 12:13:14','yyyyMMddHHmmss') union all
select "date_format(current_date,'yyyy-MM-dd HH:mm:ss')", date_format(current_date,'yyyy-MM-dd HH:mm:ss') union all
select "date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss') ",date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss')

3.unix_timestamp 和

unix_timestamp(date[, pattern]) - Converts the time to a number

这个是把时间转化为时间戳的 也就是我们常说的 时间到1970-01-01 过了多少ms,返回值bigint

from_unixtime(unix_time, format) - returns unix_time in the specified format

这个就是把时间戳转化为时间格式

select  unix_timestamp(current_date) --1671379200

select unix_timestamp('20221219','yyyyMMdd') --1671379200

select unix_timestamp('20221219','yyyyMM') --4843814400 注意这里错了,格式一定要对

select from_unixtime(1671379200,'yyyyMM')--202212

这两个函数一般联合使用。

select "from_unixtime(UNIX_TIMESTAMP('20221219','yyyyMMdd'))",from_unixtime(UNIX_TIMESTAMP('20221219','yyyyMMdd'))union all
select "from_unixtime(UNIX_TIMESTAMP('2022/12/19','yyyy/MM/dd'))",from_unixtime(UNIX_TIMESTAMP('2022/12/19','yyyy/MM/dd'))

4.date_add

date_add(start_date, num_days) - Returns the date that is num_days after start_date.
start_date is a string in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. num_days is a number. The time part of start_date is ignored.

可以是string类型的日期,或者date类型或timestamp类型,返回值为date类型

select "date_add('2022-12-12',1)",date_add('2022-12-12',1) union all
select "date_add('2022-12-12 12:00:00', 1)",date_add('2022-12-12 12:00:00', 1)union all
select "date_add('2022/12/12 12:00:00', 1)",date_add('2022/12/12 12:00:00', 1) union all
select "date_add(CURRENT_DATE,1)",date_add(CURRENT_DATE,1) union all
select "date_add(CURRENT_timestamp,1)",date_add(CURRENT_timestamp,1)

5.add_months

add_months(start_date, num_months, output_date_format) - Returns the date that is num_months after start_date.
start_date is a string or timestamp indicating a valid date. num_months is a number. output_date_format is an optional String which specifies the format for output.
The default output format is 'YYYY-MM-dd'.

这个函数第一个入参是string类型的日期,或date,或者timestamp,第二个是增加的月份可以为负数,第三个是转化的类型,最后返回值是string类型

select "add_months('2009-07-30',1)",add_months('2009-07-30',1) union all
select "add_months(current_date,1)",add_months(current_date,1) union all
select "add_months(current_timestamp,1)",add_months(current_timestamp,1) union all
select "add_months(current_date,1,'yyyy-MM-dd HH:mm:ss')",add_months(current_date,1,'yyyy-MM-dd HH:mm:ss') union all
select "add_months('2009-07-30 12:13:14',1,'yyyyMMdd')",add_months('2009-07-30 12:13:14',1,'yyyyMMdd')

其实这个函数应该是分为两部分,第一部分计算增加月份,第二部分格式化date_format

---------------这么快有人收藏那我继续更新下-----------------------

6.datediff

datediff(date1, date2) - Returns the number of days between date1 and date2
date1 and date2 are strings in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. The time parts are ignored.If date1 is earlier than date2, the result is negative.

返回值是int 类型,比较的是天数,忽略时分秒

select 'datediff(current_date(),current_timestamp())',datediff(current_date(),current_timestamp()) union all
select "datediff('2022-01-02','2022-01-03')",datediff('2022-01-02','2022-01-03') union all
select "datediff('2022-01-02',current_date)",datediff('2022-01-02',current_date)

7.month_between()

months_between(date1, date2, roundOff) - returns number of months between dates date1 and date2
If date1 is later than date2, then the result is positive. If date1 is earlier than date2, then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always an integer. Otherwise the UDF calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2.
date1 and date2 type can be date, timestamp or string in the format 'yyyy-MM-dd' or 'yyyy-MM-dd HH:mm:ss'. The result is rounded to 8 decimal places by default. Set roundOff=false otherwise.

月份比较,入参可以是string,date和timestamp,返回的是一个8精度的decimal

注意如果都是第一天或者都是最后一天,返回的是整数,否则就按差值除以31.

select "months_between('2022-12-20','2022-01-20')",months_between('2022-12-20','2022-01-20')  -- 都是20号所以是整数 11个月
union all
select "months_between('2022-12-20','2022-12-05')",months_between('2022-12-20','2022-12-05')  -- =0.48387097 =15/31
union all
select "months_between('2022-12-20','2022-10-05')",months_between('2022-12-20','2022-10-05')  -- =2.48387097=2+15/31

8.day/month/year

"day(param) - Returns the day of the month of date/timestamp, or day component of interval"
Synonyms: dayofmonth
param can be one of:
1. A string in the format of 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'.
2. A date value
3. A timestamp value
4. A day-time interval valueExample:
> SELECT day('2009-07-30') FROM src LIMIT 1;
30
Function class:org.apache.hadoop.hive.ql.udf.UDFDayOfMonth
Function type:BUILTIN

说了哪些内容。

1.和dayofmonth 相同

2.可以是string date timestamp

3.注意返回值是int

month year  这两个函数和day都差不多

9.dayofmonth/DAYOFWEEK

10.floor_day/floor_month/floor_quarter/floor_year

floor_day(param) - Returns the timestamp at a day granularity
param needs to be a timestamp value
Example:
> SELECT floor_day(CAST('yyyy-MM-dd HH:mm:ss' AS TIMESTAMP)) FROM src;
yyyy-MM-dd 00:00:00
Function class:org.apache.hadoop.hive.ql.udf.UDFDateFloorDay
Function type:BUILTIN

1. 入参必须是timestamp,必须! 其他函数date ts string 都可以就这类函数不行

2.返回值也是timestamp

其实很好记住。因为有floor_second 你不是timestamp,没有毫秒 怎么floor_second

select 'current_timestamp`()' ,`current_timestamp`()
union all select 'floor_year(current_timestamp())',floor_year(current_timestamp())
union all select 'floor_quarter(`current_timestamp`())', floor_quarter(`current_timestamp`())
union all select 'floor_month(`current_timestamp`())', floor_month(`current_timestamp`())
union all select 'floor_week(`current_timestamp`())' ,floor_week(`current_timestamp`())
union all select 'floor_day(`current_timestamp`())', floor_day(`current_timestamp`())
union all select 'floor_hour(`current_timestamp`())', floor_hour(`current_timestamp`())
union all select 'floor_minute(`current_timestamp`())', floor_minute(`current_timestamp`())
union all select 'floor_second(`current_timestamp`())', floor_second(`current_timestamp`())

hive时间和字符串互转,时间函数 其实trunc函数也可以上述的作用

1.入参比较随便string date ts 都行

2.返回值为string

3.功能比较简单只有年月季度,且只有如下几种写法

select 'current_date`()' ,date_format(`current_date`(),'yyyy-MM-dd')
union all select "trunc(`current_date`(),'YYYY')", trunc(`current_date`(),'YYYY')
union all select "trunc(`current_date`(),'YY')", trunc(`current_date`(),'YY')
union all select "trunc(`current_date`(),'YEAR')", trunc(`current_date`(),'YEAR')
union all select "trunc(`current_date`(),'MONTH')", trunc(`current_date`(),'MONTH')
union all select "trunc(`current_date`(),'MM')", trunc(`current_date`(),'MM')
union all select "trunc(`current_date`(),'MON')", trunc(`current_date`(),'MON')
union all select "trunc(`current_date`(),'QUARTER')", trunc(`current_date`(),'QUARTER')
union all select "trunc(`current_date`(),'Q')", trunc(`current_date`(),'Q')
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.csdn.net/cclovezbf/article/details/128371131,作者:cclovezbf,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

上一篇: oracle (+)学习

下一篇: hive运算时类型自动转化问题

相关文章

2024-06-12 09:24:21

Java:时间戳获取和日期时间格式化

Java:时间戳获取和日期时间格式化

2024-06-12 09:24:21
2024-06-12 09:24:21

oracle学习11-开启归档模式

oracle学习11-开启归档模式

2024-06-12 09:24:21
oracle 2024-06-07 07:32:50

java链接hive数据库实现增删改查操作

java链接hive数据库实现增删改查操作

2024-06-07 07:32:50
2024-06-03 09:09:34

【JAVA中的时间类型】

【JAVA中的时间类型】

2024-06-03 09:09:34
2024-05-31 07:36:18

java中获取当前系统日期

java中获取当前系统日期

2024-05-31 07:36:18
2024-05-29 07:56:46

hive常用命令记录

hive常用命令记录

2024-05-29 07:56:46
hadoop 2024-05-23 09:58:30

crontab任务以user用户执行hive -f命令不生效解决记录

crontab任务以user用户执行hive -f命令不生效解决记录

2024-05-23 09:58:30
hadoop 2024-05-23 09:58:30

Python使用pyhive从hive直接查询数据到dataframe

Python使用pyhive从hive直接查询数据到dataframe

2024-05-23 09:58:30
python 2024-05-23 09:58:30

msck repair table注意事项

msck repair table注意事项

2024-05-23 09:58:30
2024-05-23 09:29:21

Hive部署及常用命令

Hive部署及常用命令

2024-05-23 09:29:21
hadoop
查看更多
推荐标签

作者介绍

天翼云小翼
天翼云用户

文章

21549

阅读量

2117916

查看更多

最新文章

oracle学习11-开启归档模式

2024-06-12 09:24:21

java链接hive数据库实现增删改查操作

2024-06-07 07:32:50

hive常用命令记录

2024-05-29 07:56:46

crontab任务以user用户执行hive -f命令不生效解决记录

2024-05-23 09:58:30

字符串转日期,Cause: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.S

2024-03-26 07:47:42

hive之生成唯一id

2023-08-04 08:44:32

查看更多

热门文章

Waterdrop同步hive数据到clickhouse的异常数据导致8123端口挂掉

2023-03-31 07:57:16

linux指令:时间与日期

2023-06-19 07:03:01

hive 搭建数据仓库

2023-06-25 06:53:58

hive之生成唯一id

2023-08-04 08:44:32

hive的关系运算、逻辑预算、数学运算、数值运算、日期函数、条件函数和字符串函数的语法与使用示例详解

2023-07-06 09:39:09

hive如何实现oracle中复杂的update sql

2023-08-01 08:53:54

查看更多

热门标签

mysql MySQL redis oracle python linux Oracle leetcode
查看更多

相关产品

弹性云主机

随时自助获取、弹性伸缩的云服务器资源

天翼云电脑(公众版)

便捷、安全、高效的云电脑服务

对象存储

高品质、低成本的云上存储服务

云硬盘

为云上计算资源提供持久性块存储

查看更多

随机文章

hive——创建表不支持新增字段问题

hive的关系运算、逻辑预算、数学运算、数值运算、日期函数、条件函数和字符串函数的语法与使用示例详解

sqoop 从hive导入mysql 出现:ERROR tool.BaseSqoopTool: Error parsing arguments for export:(问题解决)

hive 搭建数据仓库

crontab任务以user用户执行hive -f命令不生效解决记录

第11章 hive常见错误及解决方案

  • 7*24小时售后
  • 无忧退款
  • 免费备案
  • 专家服务
售前咨询热线
400-810-9889转1
  • 天翼云APP
    天翼云APP
 
推荐文章
严肃的荒野  ·  浙江工商大学
1 年前
卖萌的滑板  ·  地下偶像第十四集原版-西瓜视频搜索
1 年前
飘逸的哑铃  ·  “素媛案”罪犯即将出狱,称想见受害者,多次表现过盛欲望_澎湃号·湃客_澎湃新闻-The Paper
2 年前
鼻子大的酱牛肉  ·  叶罗丽:新仙子古风造型像罗丽,金王子打造了曼多拉的最美造型_公主_魔法_怪兽
2 年前
huangendabbs  ·  人民日报探析资本主义系统性危机:根源、如何看待-新华网
4 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号