hive/spark
1.時間表示
select current_date -- 當前時間(日)
select unix_timestamp() -- 當前時間戳
select from_unixtime(unix_timestamp(),'yyy-MM-dd HH:mm:ss') -- 時間戳轉日期
2.常用函數
select date_sub(current_date,3) -- 當前日期-3
select date_add(current_date,3) -- 當前日期+3
select year(current_date)
select month(current_date)
select quarter(current_date) -- 季度
select weekofyear(current_date) -- 日期所在年的第幾周
select day(current_date)
select minute('2020-12-10 12:19:01')
select second('2020-12-10 12:19:01')
select next_day(current_date,'MO') -- 當前日期的下一個周一
select next_day(current_date,'tue') -- 當前日期的下一個周二
select next_day(current_date,'wed') -- 當前日期的下一個周三
select datediff('2020-12-17','2020-12-10') -- 日期差(天數)
3.周/月
績效月:上個月20號到本月19號
select concat(substr(date_sub(current_date,day(current_date)+1), 1,7), '-20')
select concat(substr(current_date, 1,7),'-19')
自然周:
select concat(date_sub(next_day(current_date,'MO'),7),'~',date_sub(next_day(current_date,'MO'),1))
自然月:
select concat(add_months(trunc(current_date,'MM'),-1),'~',add_months(trunc(current_date,'MM'),0))
4.trunc函數:(數字/日期截取),hive中僅支持日期截取,所支持的格式為MONTH/MON/MM, YEAR/YYYY/YY
select trunc('2020-04-12 20:01:00','yyyy') -- 2020-01-01
select trunc('2020-04-12 20:01:00','mm') -- 2020-04-01
5.pmod函數:取余函數,第二個參數需設定為星期一,第一個參數才能正確展示為本周的星期幾
select pmod(datediff('2020-12-16','2020-12-07'),7)+1 -- 2020-12-16 是星期三
select pmod(datediff('2020-12-16','2020-12-07')+1,7) -- 2020-12-16 是星期三
presto
1.常用函數
select current_timezone() -- 當前時區
select current_date -- 當前日期
select current_time -- 當前時間加時區
select now()
select localtime -- 當前查詢時間
select current_timestamp -- 帶時區的時間戳
select localtimestamp -- 不帶時區的時間戳
select typeof(localtimestamp)
select to_unixtime(current_timestamp) -- 當前unix時間戳
select parse_duration('28800s') -- 任意數字轉換成hh:mm:ss
select date_parse('2020-12-17 20:00:00','%Y-%m-%d %H:%i:%s')
select typeof(date_parse('2020-12-17 20:00:00','%Y-%m-%d %H:%i:%s')) -- 字符串解析為timstamp ??? 什么時候用yyyy-MM-dd HH:mm:ss ???只寫到日期為何不能自動以0補足
select typeof(date_format(timestamp '2020-12-17 20:00:00','%Y-%m-%d %H:%i:%s')) -- 指定格式的時間戳轉成字符串
select to_milliseconds(interval '8' hour) -- 28800000 距離當天零時過去的毫秒數
/*** to_date()函數要求小時在0~11內,超過此范圍會提示:Value 12 for hourOfHalfday must be in the range [0,11] ***/
select to_date('2019-07-19 13:01:01','yyyy-mm-dd hh:mi:ss') -- error
select to_date('2011-12-08 12:01:01','yyyy-mm-dd hh:mi:ss')
select to_date('2011-12-08 13:01:51','yyyy-mm-dd hh24:mi:ss') -- 小時改成24小時制即可
select to_unixtime(timestamp'2016-06-27 21:00:00') --日期轉時間戳
自然周:
select concat(cast(date_add('day',-7,date_trunc('week',current_date)) as varchar),'~',cast(date_add('day',0,date_trunc('week',current_date)) as varchar))
自然月:
select concat(cast(date_add('month',-1,date_trunc('month',current_date)) as varchar),'~',cast(date_add('month',0,date_trunc('month',current_date))as varchar))
2.日期時間運算
select timestamp '2012-08-08 10:00:00' + interval '2' day
select date '2019-07-01'+interval '2' year
select date '2012-08-08' + interval '2' MONTH
運算符 | 示例 | 結果 |
---|---|---|
+ | date '2012-08-08' + interval '2' day | 2012-08-10 |
+ | time '01:00' + interval '3' hour | 04:00:00 |
+ | timestamp '2012-08-08 01:00' + interval '29' hour | 2012-08-09 06:00:00.000 |
+ | timestamp '2012-10-31 01:00' + interval '1' month | 2012-11-30 01:00:00.000 |
+ | interval '2' day + interval '3' hour | 2 03:00:00.000 |
+ | interval '3' year + interval '5' month | 3-5 |
- | date '2012-08-08' - interval '2' day | 2012-08-06 |
- | time '01:00' - interval '3' hour | 22:00:00.000 |
- | timestamp '2012-08-08 01:00' - interval '29' hour | 2012-08-06 20:00:00.000 |
- | timestamp '2012-10-31 01:00' - interval '1' month | 2012-09-30 01:00:00.000 |
- | interval '2' day - interval '3' hour | 1 21:00:00.000 |
- | interval '3' year - interval '5' month | 2-7 |
3.截取函數date_trunc(unit,x)——>[same as input]
栗子:date_trunc(unit,2001-08-22 03:04:05.321)
單位unit | 結果 |
---|---|
second | 2001-08-22 03:04:05.000 |
minute | 2001-08-22 03:04:00.000 |
hour | 2001-08-22 03:00:00.000 |
day | 2001-08-22 00:00:00.000 |
week | 2001-08-20 00:00:00.000 |
month | 2001-08-01 00:00:00.000 |
quarter | 2001-07-01 00:00:00.000 |
year | 2001-01-01 00:00:00.000 |
4.間隔函數
date_add(unit,value,timestamp) —> [same as input]
date_diff(unit_timestamp1,timestamp2) —> bigint 兩個時間間隔
支持的類型 | |
---|---|
unit | millisecond、second、minute、hour、day、week、month、quarter、year |
5.持續時間函數
parse_duration(string) —> interval
select parse_duration('28800s') -- 0 08:00:00.000
select parse_duration('3.5d') --3 12:00:00.000
select parse_duration('2.5m') --0 00:02:30.000
unit | 解釋 |
---|---|
ns | 納秒 |
us | 微秒 |
ms | 毫秒 |
s | 秒 |
m | 分 |
h | 時 |
d | 天 |
6.抽取函數
select extract(field from x)
select extract(year from current_date)
field | 描述 |
---|---|
year | year() |
quarter | quarter() |
month | month() |
week | week() |
day、day_of_month | day() |
day_of_week、dow | day_of_week() |
day_of_year、doy | day_of_year() |
year_of_week、yow | year_of_week() |
hour | hour() |
minute | minute() |
second | second() |
timezone_hour | timezone_hour() |
timezone_minute | timezone_minute() |