hive日期

1.next_day函数
取当前天的下一个周一

hive (gmall)> select next_day('2021-05-28','MO');
2021-05-31

2.取当前周的周一

hive (gmall)> select date_add(next_day('2021-05-28','MO'),-7);
2020-11-30

3.取当前周的周一和周日

select
last_mo,
date_add(last_mo,6) this_sun
from(
select date_add(next_day('2021-05-28','MO'),-7) last_mo
) m ;

select date_add(next_day(current_date(),'MO'),-7) this_mo, date_add(next_day(current_date(),'MO'),-1) this_sun;

4.自然周

hive> select cast(year(date_sub(next_day(date_sub('2022-08-02',1),'MO'),4))*100+weekofyear('2022-08-02') as string);
OK
202231
Time taken: 1.078 seconds, Fetched: 1 row(s)

5.月份

返回当前时间下再增加num_months个月的日期   add_months(string start_date, int num_months)
返回值:string
hive> select add_months('2017-02-10', 2);
返回结果:
2017-04-10

补充:date_add函数,datediff函数

hive (gmall)>select date_add('2021-03-20',-1);

2020-03-19
hive (gmall)>select datediff('2021-03-20','2021-03-28');
-8
1. 获取当前时间:yyyy-mm-dd

SELECT CURRENT_DATE;

2. 获取当月第一天时间:yyyy-mm-dd

select date_sub(current_date,dayofmonth(current_date)-1);

3. 获取下个月第一天时间:yyyy-mm-dd

select add_months(date_sub(current_date,dayofmonth(current_date)-1),1);

4. 获取当月第几天:yyyy-mm-dd

select dayofmonth(current_date);

5. 获取当前日期所在月月末日期:yyyy-mm-dd

select last_day(current_date);

6. 获取当前日期本周一:yyyy-mm-dd

select date_sub(next_day(CURRENT_DATE,'MO'),7);

7. 获取当前日期上周一:yyyy-mm-dd

select date_sub(next_day(CURRENT_DATE,'MO'),14);

8. 获取当前日期上周日:yyyy-mm-dd

select date_sub(next_day(CURRENT_DATE,'MO'),8);

9. 获取当前日期本周二:yyyy-mm-dd(获取其他周几调整最后参数)

select date_sub(next_day(CURRENT_DATE,'MO'),6);

10. 获取当前日期上周二:yyyy-mm-dd(获取其他周几调整最后参数)

select date_sub(next_day(CURRENT_DATE,'MO'),13) ;

11. 获取当前时间的前/后几个月时间:yyyy-mm-dd(调整最后参数)

select add_months(CURRENT_DATE,-3);

12. 开始结束时间相差天数

datediff(string enddate, string startdate);

13. 从开始时间startdate加上days

date_add(string startdate, int days)

14. 从开始时间startdate减去days

date_sub(string startdate, int days);

15. 获取上季度初日期

select add_months(concat(year(CURRENT_DATE),'-',substr(concat('0',floor((month(CURRENT_DATE)+2)/3)*3+1),-2),'-01'),-6);

16. 获取本季度初日期

select add_months(concat(year(CURRENT_DATE),'-',substr(concat('0',floor((month(CURRENT_DATE)+2)/3)*3+1),-2),'-01'),-3);

17.取上个季度同今天时间

select add_months(CURRENT_DATE,-3);

18.去年本季度开始时间

select add_months(concat(year(CURRENT_DATE),'-',substr(concat('0',floor((month(CURRENT_DATE)+2)/3)*3+1),-2),'-01'),-15);

19.去年本季度结束时间

select add_months(CURRENT_DATE,-12);
mysql日期
-- 1、interval的说明:
1.1、当函数使用时,即interval(),为比较函数,如:interval(10,1,3,5,7); 结果为4;
原理:10为被比较数,后面1,3,5,7为比较数,将后面四个依次与10比较,看后面数字组有多少个少于10,则返回其个数。前提是后面数字组为从小到大排列,否则返回结果0。

1.2、当关键词使用时,表示为设置时间间隔,常用在date_add()与date_sub()函数里,
如:interval 1 day ,解释为将时间间隔设置为1天。

3、本月第一天
select date_add(curdate(), interval - day(curdate()) + 1 day);

今天是当月的第几天:SELECT DAYOFMONTH( NOW());
4、本月最后一天
select last_day(curdate());
5、上月第一天
select date_add(curdate()-day(curdate())+1,interval -1 month);
6、上月最后一天
select last_day(date_sub(now(),interval 1 month));
7、下月第一天
select date_add(curdate()-day(curdate())+1,interval 1 month);
8、下月最后一天
select last_day(date_sub(now(),interval -1 month));
9、本月天数
select day(last_day(curdate()));
10、上月今天的当前日期
select date_sub(curdate(), interval 1 month);
11、上月今天的当前时间(时间戳)
select unix_timestamp(date_sub(now(),interval 1 month));
12、获取当前时间与上个月之间的天数
select datediff(curdate(), date_sub(curdate(), interval 1 month));
13、今天
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') AS '今天开始';
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 23:59:59') AS '今天结束';
14、昨天
SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 00:00:00') AS '昨天开始';
SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 DAY), '%Y-%m-%d 23:59:59') AS '昨天结束';
15、上周
SELECT DATE_FORMAT( DATE_SUB( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 1 WEEK), '%Y-%m-%d 00:00:00') AS '上周一';
SELECT DATE_FORMAT( SUBDATE(CURDATE(), WEEKDAY(CURDATE()) + 1), '%Y-%m-%d 23:59:59') AS '上周末';
16、本周
SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-1), '%Y-%m-%d 00:00:00') AS '本周一';
SELECT DATE_FORMAT( SUBDATE(CURDATE(),DATE_FORMAT(CURDATE(),'%w')-7), '%Y-%m-%d 23:59:59') AS '本周末';

-- 上面的本周算法会有问题,因为mysql是按照周日为一周第一天,如果当前是周日的话,会把时间定为到下一周.
SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), '%Y-%m-%d 00:00:00') AS '本周一';
SELECT DATE_FORMAT( DATE_ADD(SUBDATE(CURDATE(), WEEKDAY(CURDATE())), INTERVAL 6 DAY), '%Y-%m-%d 23:59:59') AS '本周末';
17、上月
SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01 00:00:00') AS '上月初';
SELECT DATE_FORMAT( LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), '%Y-%m-%d 23:59:59') AS '上月末';
18、本月
SELECT DATE_FORMAT( CURDATE(), '%Y-%m-01 00:00:00') AS '本月初';
SELECT DATE_FORMAT( LAST_DAY(CURDATE()), '%Y-%m-%d 23:59:59') AS '本月末';
19、本年第一天
SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY)  

或

concat(year(now()),'-01-01')//当前年份的第一天
concat(year(now()),'-12-31')//当前年份的最后一天
20、时间差值换算
SELECT 
	TIME_TO_SEC(TIMEDIFF('2018-09-30 19:38:45', '2018-08-23 10:13:01')) AS DIFF_SECOND1, -- 秒
	UNIX_TIMESTAMP('2018-09-30 19:38:45')-UNIX_TIMESTAMP('2018-08-23 10:13:01') AS DIFF_SECOND2, -- 秒
	TIMESTAMPDIFF(SECOND,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_SECOND3, -- 秒
	TIMESTAMPDIFF(MINUTE,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_MINUTE, -- 分
	TIMESTAMPDIFF(HOUR,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_HOUR, -- 小时
	TIMESTAMPDIFF(DAY ,'2018-08-23 10:13:01','2018-09-30 19:38:45') AS DIFF_DATE1, -- 天
	DATEDIFF('2018-09-30 19:38:45','2018-08-23 10:13:01') AS DIFF_DATE2, -- 天
	TIMESTAMPDIFF(MONTH,'2018-08-23 10:13:01','2018-09-25 19:38:45') AS DIFF_MONTH, -- 月
	TIMESTAMPDIFF(YEAR,'2018-08-23 10:13:01','2020-07-25 19:38:45') AS DIFF_YEAR -- 年
FROM DUAL;
21、日期增加年,月,天,小时,分,秒
select date_add(日期, interval 1 day); 日期加天
select date_add(日期, interval 1 hour); 日期加小时
select date_add(日期, interval 1 minute); 日期加分
select date_add(日期, interval 1 second);日期加秒
select date_add(日期, interval 1 microsecond); 日期加微秒
select date_add(日期, interval 1 week); 日期加周
select date_add(日期, interval 1 month); 日期加月
select date_add(日期, interval 1 quarter); 日期加季度
select date_add(日期, interval 1 year); 日期加年
22、当月的所有天数循环(输入日期自动换算当月天数) 
SELECT ADDDATE(y.first, x.d - 1) as d 
FROM (
	SELECT 1 AS d UNION ALL
	SELECT 2 UNION ALL
	SELECT 3 UNION ALL
	SELECT 4 UNION ALL
	SELECT 5 UNION ALL
	SELECT 6 UNION ALL
	SELECT 7 UNION ALL
	SELECT 8 UNION ALL
	SELECT 9 UNION ALL
	SELECT 10 UNION ALL
	SELECT 11 UNION ALL
	SELECT 12 UNION ALL
	SELECT 13 UNION ALL
	SELECT 14 UNION ALL
	SELECT 15 UNION ALL
	SELECT 16 UNION ALL
	SELECT 17 UNION ALL
	SELECT 18 UNION ALL
	SELECT 19 UNION ALL
	SELECT 20 UNION ALL
	SELECT 21 UNION ALL
	SELECT 22 UNION ALL
	SELECT 23 UNION ALL
	SELECT 24 UNION ALL
	SELECT 25 UNION ALL
	SELECT 26 UNION ALL
	SELECT 27 UNION ALL
	SELECT 28 UNION ALL
	SELECT 29 UNION ALL
	SELECT 30 UNION ALL
	SELECT 31
) x,
(
	SELECT '2019-09-05' - INTERVAL DAY('2019-09-05') - 1 DAY AS first, DAY(LAST_DAY('2019-09-05')) AS last 
) y
WHERE x.d <= y.last
23、模拟12个月日期
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-01-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-01-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-02-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-02-28'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-03-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-03-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-04-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-04-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-05-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-05-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-06-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-06-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-07-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-07-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-08-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-08-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-09-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-09-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-10-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-10-31'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-11-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-11-30'),'%Y-%m-%d') END_V
UNION
SELECT STR_TO_DATE(CONCAT(YEAR(now()),'-12-01'),'%Y-%m-%d') BEGIN_V,STR_TO_DATE(CONCAT(YEAR(now()),'-12-31'),'%Y-%m-%d') END_V
24、
    #当年第一天:  
    SELECT DATE_SUB(CURDATE(),INTERVAL dayofyear(now())-1 DAY);  
      ```

```sql
#当年最后一天:  
SELECT concat(YEAR(now()),'-12-31');    
  
#当前week的第一天:    
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 1 DAY);  
  
#当前week的最后一天:    
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) - 5 DAY);  
  
#前一week的第一天:    
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 8 DAY);  
  
#前一week的最后一天:    
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 2 DAY);  
  
#前两week的第一天:    
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 15 DAY);  
  
#前两week的最后一天:    
select date_sub(curdate(),INTERVAL WEEKDAY(curdate()) + 9 DAY);  
  
#当前month的第一天:    
SELECT concat(date_format(LAST_DAY(now()),'%Y-%m-'),'01');  
  
#当前month的最后一天:    
SELECT  LAST_DAY(now());  
  
#前一month的第一天:    
SELECT concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01');  
  
#前一month的最后一天:    
SELECT LAST_DAY(now() - interval 1 month);  
  
#前两month的第一天:    
SELECT concat(date_format(LAST_DAY(now() - interval 2 month),'%Y-%m-'),'01');  
  
#前两month的最后一天:    
SELECT  LAST_DAY(now() - interval 2 month);  
  
#当前quarter的第一天:    
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM  CURDATE()),1) + interval QUARTER(CURDATE())*3-3 month),'%Y-%m-'),'01');   
  
#当前quarter的最后一天:    
select LAST_DAY(MAKEDATE(EXTRACT(YEAR  FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-1 month);  
  
#前一quarter的第一天:    
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-6 month),'%Y-%m-'),'01');  
  
#前一quarter的最后一天:    
select  LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-4 month);  
  
#前两quarter的第一天:    
select concat(date_format(LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-9 month),'%Y-%m-'),'01');  
  
#前两quarter的最后一天:    
select LAST_DAY(MAKEDATE(EXTRACT(YEAR FROM CURDATE()),1) + interval QUARTER(CURDATE())*3-7 month);