函数 | 参数格式 | 解释 |
---|---|---|
from_unixtime | from_unixtime(bigint unixtime[, string format]) | 将unix时间戳转换为当前所在时区的字符串时间,格式为"yyyy-MM-dd HH:mm:ss" |
unix_timestamp | unix_timestamp() | 以秒为单位获取当前的Unix时间戳。 |
unix_timestamp | unix_timestamp(string date) | 将格式为"yyyy-MM-dd HH:mm:ss"的字符串时间转换为Unix时间戳 |
unix_timestamp | unix_timestamp(string date, string pattern) | 将格式为pattern的字符串时间转换为Unix时间戳,若转换失败则返回0 |
to_date | to_date(string timestamp) | 传入字符串时间,返回"yyyy-MM-dd"日期 |
year | year(string date) | 返回给定字符串时间所在的年,例如:year(“1970-01-01 00:00:00”) = 1970; year(“1970-01-01”) = 1970 |
quarter | quarter(date/timestamp/string) | 返回给时间所在的季度,例如: year(“1970-04-01”) =2 |
month | month(string date) | 返回给定时间所在的月,例如: month(“1970-05-01”) = 5 |
day | day(string date) dayofmonth(date) | 返回给定时间在当月的第几天,例如: day(“1970-05-01”) = 1 |
minute | minute(string date) | 返回给定时间所在分钟,例如: minute(“2009-07-30 12:58:59”) = 58 |
second | second(string date) | 返回给定时间所在秒数,例如: second(“2009-07-30 12:58:59”) = 59 |
weekofyear | weekofyear(string date) | 返回给定时间在该年的第几周,例如: weekofyear(“1970-11-01”) = 44 |
extract | extract(field FROM source) | 将给定的field函数应用在时间source。例如:select extract(month from “2016-10-20”)=10;extract(dayofweek from “1970-11-01”)=44 |
datediff | datediff(string enddate, string startdate) | 计算enddate与startdate相差的天数 |
date_add | date_add(date/timestamp/string startdate, tinyint/smallint/int days) | 将startdate加上给定的天数并返回新的日期 |
from_utc_timestamp | from_utc_timestamp({any primitive type} ts, string timezone) | 将UTC时间戳转换为给定时区的时间,例如from_utc_timestamp(timestamp ‘1970-01-30 16:00:00’,‘PST’)的返回值为"1970-01-30 08:00:00" |
to_utc_timestamp | to_utc_timestamp({any primitive type} ts, string timezone) | 将给定时区的时间转换为UTC时间戳,例如to_utc_timestamp(timestamp ‘1970-01-30 16:00:00’,‘PST’)的返回值为"1970-01-31 00:00:00" |
current_date | current_date | 查询当前日期,同一查询中current_date都一样 |
current_timestamp | current_timestamp | 查询当前时间,同一查询中current_timestamp都一样 |
add_months | add_months(string start_date, int num_months, output_date_format) | 返回start_date加上num_months个月后的时间,并指定格式为output_date_format |
last_day | last_day(string date) | 返回date所在月的最后一天的日期 |
next_day | next_day(string start_date, string day_of_week) | 返回位于start_date后,且所在星期天数为day_of_week的日期。例如:next_day(‘2015-01-14’,‘TU’)= “2015-01-20”(day_of_week可以为 Mo, tue, FRIDAY等形式) |
trunc | trunc(string date, string format) | 将日期date截断到format格式,例如trunc(‘2015-03-17’,‘MM’)= “2015-03-01”。(支持的格式有:MONTH / MON / MM,YEAR / YYYY / YY) |
months_between | months_between(date1, date2) | 计算date1与date2相差的月数,精确到小数。例如months_between(‘1997-02-28 10:30:00’, ‘1996-10-30’) = 3.94959677 |
date_format | date_format(date/timestamp/string ts, string fmt) | 将时间戳转换为指定格式,例如:date_format(‘2015-04-08’, ‘y’) = ‘2015’ |
Hive:常用系统函数-时间函数
最新推荐文章于 2024-02-21 22:57:34 发布