Hive 日期函数

1. 获取相关

语法格式: unix_timestamp()

返回类型: bigint

函数描述:以秒为单位获取当前时区的 Unix 时间戳。

> select unix_timestamp();
1654323887

语法格式: extract(field FROM source)

返回类型: bigint

函数描述: 从源中检索诸如天或小时之类的字段。源必须是日期、时间戳、间隔或可以转换为日期或时间戳的字符串。支持的字段包括:日、星期几、小时、分钟、月、季度、秒、周和年。

> select extract(year from '2022-06-04 14:24:47');
2022
> select extract(month from '2022-06-04 14:24:47');
6
> select extract(week from '2022-06-04 14:24:47');
22
> select extract(day from '2022-06-04 14:24:47');
4
> select extract(hour from '2022-06-04 14:24:47');
14
> select extract(minute from '2022-06-04 14:24:47');
24
> select extract(second from '2022-06-04 14:24:47');
47
> select extract(dayofweek from '2022-06-04 14:24:47');
7 # 错一天

语法格式: current_date

返回类型: date

函数描述: 返回当前日期。

> select current_date;
2022-06-04

> select current_date();
2022-06-04

语法格式: current_timestamp

返回类型: timestamp

函数描述: 返回当前时间戳。

> select current_timestamp;
2022-06-04 14:36:03.322000000

语法格式: last_day(string date)

返回类型: string

函数描述: 返回日期所属月份的最后一天。

> select last_day('2022-06-04 14:24:47');
2022-06-30

> select last_day('2022-06-04');
2022-06-30

语法格式: next_day(string start_date, string day_of_week)

返回类型: string

函数描述: 返回晚于 start_date 并命名为 day_of_week 的第一个日期。

> select next_day('2022-06-04 14:24:47','Sun')
2022-06-05

> select next_day('2022-06-04 14:24:47','TU')
2022-06-07

语法格式: trunc(string date, string format)

返回类型: string

函数描述: 返回截断为格式指定的单位的日期。

> select trunc('2022-06-04 14:24:47','MM'); # MM、MON、MONTH
2022-06-01

> select trunc('2022-06-04 14:24:47','YEAR');
2022-01-01

语法格式: months_between(date1, date2)

返回类型: double

函数描述: 返回日期 date1 和 date2 之间的月数。如果 date1 晚于 date2,则结果为正。如果 date1 早于 date2,则结果是否定的。如果 date1 和 date2 是一个月中的同一天或两个月的最后一天,则结果始终为整数。date1 和 date2 类型可以是日期、时间戳或字符串。

> select months_between('2022-06-04 14:24:47','2022-09-01');
-2.88385342

> select months_between('2022-09-04','2022-09-01');
0.09677419

2. 转换相关

语法格式: from_unixtime(bigint unixtime[, string format])

返回类型: bigint

函数描述:将到 unix 纪元的秒数转换为表示当前系统时区中那个时刻的时间戳的字符串。

> select from_unixtime(1654323887,'yyyyMMdd');
20220604

> select from_unixtime(1654323887,'yyyy-MM-dd HH:mm:ss');
2022-06-04 14:24:47

语法格式: unix_timestamp(string date)

返回类型: bigint

函数描述:使用默认时区和默认值将格式中的时间字符串转换 yyyy-MM-dd HH:mm:ss为 Unix 时间戳(以秒为单位),如果失败则返回 0。

> select unix_timestamp('2022-06-04 14:24:47');
1654323887

语法格式: unix_timestamp(string date, string pattern)

返回类型: bigint

函数描述:将具有给定模式的时间字符串转换为 Unix 时间戳(以秒为单位),如果失败返回 0。

> select unix_timestamp('20220604 14:24:47', 'yyyyMMdd HH:mm:ss');
1654323887

语法格式: to_date(string timestamp)

返回类型: date

函数描述:返回时间日期的日期部分,返回一个日期对象。

> select to_date('2022-06-04 14:24:47');
2022-06-04

> select to_date('2022-06-04');
2022-06-04

语法格式: year(string date)

返回类型: int

函数描述:返回日期时间的年份部分。

> select year('2022-06-04 14:24:47');
2022

> select year('2022-06-04');
2022

语法格式: quarter(date/timestamp/string)

返回类型: int

函数描述:返回日期时间所在一年中的季度,返回值范围1~4。

> select quarter('2022-06-04 14:24:47');
2

> select quarter('2022-06-04');
2

语法格式: month(string date)

返回类型: int

函数描述:返回日期时间的月份部分。

> select month('2022-06-04 14:24:47');
6

> select month('2022-06-04');
6

语法格式: day(string date) dayofmonth(date)

返回类型: int

函数描述:返回日期时间的日期部分。

> select day('2022-06-04 14:24:47');
4

> select day('2022-06-04');
4

> select dayofmonth('2022-06-04');
4

语法格式: hour(string date)

返回类型: int

函数描述:返回时间的小时。

> select hour('2022-06-04 14:24:47');
14

语法格式: minute(string date)

返回类型: int

函数描述:返回时间的分钟。

> select hour('2022-06-04 14:24:47');
24

语法格式: second(string date)

返回类型: int

函数描述:返回时间的秒。

> select hour('2022-06-04 14:24:47');
47

语法格式: weekofyear(string date)

返回类型: int

函数描述:返回日期时间在一年的第几周。

> select weekofyear('2022-06-04 14:24:47');
22

语法格式: from_utc_timestamp({*any primitive type*} ts, string timezone)

返回类型: timestamp

函数描述:将 UTC 中的时间戳转换为给定的时区。

> select from_utc_timestamp('2022-06-04 00:00:00','PRC');
2022-06-04 08:00:00

语法格式: to_utc_timestamp({*any primitive type*} ts, string timezone)

返回类型: timestamp

函数描述:将给定时区中的时间戳转换为 UTC。

> select to_utc_timestamp('2022-06-04 08:00:00','PRC');
2022-06-04 00:00:00

语法格式: date_format(date/timestamp/string ts, string fmt)

返回类型: string

函数描述:将日期/时间戳/字符串转换为日期格式 fmt 指定的格式的字符串值。

> select date_format('2022-06-04 14:24:47','y')
2022

> > select date_format('2022-06-04 14:24:47','M')
6

3. 计算相关

语法格式: datediff(string enddate, string startdate)

返回类型: int

函数描述:返回从 startdateenddate 的天数。

> select datediff('2022-06-01 14:24:47', '2022-06-04 14:24:47');
-3

> select datediff('2022-06-01', '2022-06-04');
-3

语法格式: date_add(date/timestamp/string startdate, tinyint/smallint/int days)

返回类型: date

函数描述:返回 startdate 增加 days 后的日期。

> select date_add('2022-06-04', 3);
2022-06-07

> select date_add('2022-06-04', -3);
2022-06-01

语法格式: date_sub(date/timestamp/string startdate, tinyint/smallint/int days)

返回类型: date

函数描述:返回 startdate 减去 days 后的日期。

> select date_sub('2022-06-04', 3);
2022-06-01

> select date_sub('2022-06-04', -3);
2022-06-07

语法格式: add_months(string start_date, int num_months)

返回类型: string

函数描述:返回 start_date 之后 num_months 的日期。

> select add_months('2022-06-04 14:24:47', 3);
2022-09-04

> select add_months('2022-06-04', 3);
2022-09-04

❤️ END ❤️

Original: https://blog.csdn.net/weixin_47243236/article/details/125123524
Author: JOEL-T99
Title: Hive 日期函数

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/818370/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球