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

这篇具有很好参考价值的文章主要介绍了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)

hive字符串转日期,hive,hive,日期

 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') 

hive字符串转日期,hive,hive,日期

 3.unix_timestamp 和from_unixtime

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'))

hive字符串转日期,hive,hive,日期

 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)

hive字符串转日期,hive,hive,日期

 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')

hive字符串转日期,hive,hive,日期

 其实这个函数应该是分为两部分,第一部分计算增加月份,第二部分格式化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)

hive字符串转日期,hive,hive,日期

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

hive字符串转日期,hive,hive,日期

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

同8

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字符串转日期,hive,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')

 hive字符串转日期,hive,hive,日期

11.timestamp

有时候我们有个string或者date类型的 2022-01-01,此时又需要timestamp类型,

cast('2022-01-01' as timestamp)有点麻烦

直接timestamp('2022-01-01') hive字符串转日期,hive,hive,日期

 文章来源地址https://www.toymoban.com/news/detail-781693.html

到了这里,关于hive时间和字符串互转,时间函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SQL Server日期时间字符串的处理和转换

    在SQL Server中,您可以使用T-SQL函数进行日期时间字符串的处理和转换。要判断一个日期字符串是否包含时间信息,可以使用T-SQL内置的函数CONVERT和TRY_CONVERT,并指定时间格式。 例如,假设有一个名为date_string的日期字符串,您可以使用以下代码来判断它是否包含时间信息: 如

    2024年02月16日
    浏览(35)
  • Python - 字符串转日期时间,格式的处理以及时间加减计算

    1,字符串转日期 运行结果: 注意事项: a, 日期时间转换时,读取的格式要和给定的格式一样,否则会因为无法匹配而报错 【格式1 和 格式2 需要保持一直】 b, 转换之后的日期格式会自动加上\\\'秒\\\'位 2,时间格式处理 根据自己的需求,通过strftime( )函数内的控制符把日期时间格

    2024年02月12日
    浏览(42)
  • Hive字符串函数讲解

    Hive 提供了许多内建的字符串函数来处理文本数据。以下是您提到的字符串函数的详细讲解,包括案例和使用注意事项: LENGTH() 功能 :返回字符串的长度。 语法 : LENGTH(string) 案例 : SELECT LENGTH(\\\'Hello World\\\');  结果为  11 注意事项 :如果输入是 NULL,则返回结果也是 NULL。 U

    2024年01月21日
    浏览(33)
  • 前端中不同格式的日期相互转换(字符串、时间戳)js相关

    在项目中遇到了,需要实现字符串和Unix时间戳的相互转换,随手记录一下。 我使用的组件库为Naive UI,涉及到的组件为日期选择器(Date Picker)。作者在文档中写道: 实话说我不喜欢这个 feature,因为多数情况下,传递时间字符串不是个最佳实践。但是现实世界是复杂的,我

    2024年02月02日
    浏览(54)
  • Java中验证日期时间字符串是否合法的几种方式

    第一种,JDK8之前用SimpleDateFormat类 可以使用SimpleDateFormat类来验证日期时间的格式和有效性。 首先,可以创建SimpleDateFormat对象,然后使用该对象的parse()方法来验证日期时间字符串的格式和有效性。如果该方法抛出异常,则表示日期时间字符串不符合指定的格式,而如果该方法

    2024年02月04日
    浏览(40)
  • Hive字符串函数-空格处理

    平常我们在数据开发的过程中,字符串函数里面包含空格会导致数据的一致性被破坏,造成我们的开发脚本出错,所以我们在数据预处理的时候,有两种情况需要考虑,一种是字符串里面空格的内容是否需要清洗,另一种字符串长度缺少是否需要用空格填充。 语法:trim(str

    2023年04月08日
    浏览(29)
  • Hive字符串截取函数substr详解

    Hive中的substr函数可以用来截取字符串的一部分,并返回截取后的结果。该函数有三个参数:第一个参数是要截取的字符串,第二个参数是截取的起始位置(从1开始),第三个参数是截取的长度。 语法: substr(str, pos, len) 举个例子,假设有一个字符串 \\\"Hello World\\\" ,我们想截取

    2024年02月03日
    浏览(43)
  • hive中时间戳与时间字符串相互转换的方法教程

    时间戳是数据库常用的存放日期的形式之一,表示从 UTC 时间’1970-01-01 00:00:00’开始到现在的秒数,与常规时间格式如 ‘2018-01-01 00:00:00’可以相互转换,方法如下。 一、unix_timestamp 函数用法 1、unix_timestamp() 返回当前时间戳。另外,current_timestamp() 也有同样作用。 2、unix_ti

    2024年02月13日
    浏览(38)
  • 【MySQL】MySQL 数据类型,数值、日期和时间、字符串类型,创建数据表,删除数据表

    作者简介: 辭七七,目前大一,正在学习C/C++,Java,Python等 作者主页: 七七的个人主页 文章收录专栏: 七七的闲谈 欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖 MySQL 中定义数据字段的类型对你数据库的优化是非常重要的。 MySQL 支持多种类型,大致可以分为三类:数值、日

    2024年02月15日
    浏览(52)
  • 在SQL中,可以使用不同的函数来转换字符串日期格式。以下是一些常用的函数:

    1. STR_TO_DATE(): 将字符串转换为日期格式。它接受两个参数:要转换的字符串和日期格式。 示例: 这将把字符串 ‘2023-07-04’ 转换为日期格式,并返回结果作为 converted_date。 2. CAST(): 将字符串转换为日期格式。它接受两个参数:要转换的字符串和目标数据类型。 示例: 这将把

    2024年02月04日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包