impala 下的SQL時間函數


#把時間轉化成時間戳
select cast('1966-07-30' as timestamp);
select cast('1985-09-25 17:45:30.005' as timestamp);
select cast('08:30:00' as timestamp);


#取月份 無效月份為null
select hour('1970-01-01 15:30:00'),hour('1970-01-01 27:30:00');


#一周的第幾天
select dayofweek('2004-06-13');


#英文下的星期幾
select dayname('2004-06-13');


#兩個時間差
select datediff('2019-11-10','2019-11-20');


把時間戳轉換成秒數
select  unix_timestamp(now())


把秒數轉成時間戳
select from_unixtime(cast(cast(1000.0 as decimal) as bigint));


把字符串轉換成時間戳
cast('2019-10-14 18:00:41' as timestamp)


#增加月份
select now(), add_months(now(), 2);
select now(), add_months(now(), -1);


#當前時間
select now(), current_timestamp();


#加6小時
select now() as right_now,date_add(now(), interval 6 hours) as in_6_hours;


#加三周
select now() as right_now,date_add(now(), interval 3 weeks) as in_3_weeks;


#加三個月
select date_add(cast('2016-01-31' as timestamp), interval 3 months) as 'april_31st';


#截取年份
select date_part('year',now()) as current_year;


#截取小時
select date_part('hour',now()) as hour_of_day;


#距現在之前的第七天
select now() as right_now,date_sub(now(), 7) as last_week;


#距現在之后第7天日期
select now() as right_now,date_sub(now(), -7) as last_week;


#前3周的那一天
select now() as right_now,date_sub(now(), interval 3 weeks) as 3_weeks_ago;


#6個小時前
select now() as right_now,date_sub(now(), interval 6 hours) as 6_hours_ago;


#上一個月
select date_sub(cast('2016-05-31' as timestamp), interval 1 months) as 'april_31st';


#相差的天數
select now() as right_now, datediff(now() + interval 5 days,now()) as in_5_years;


#取天數
select now(), day(now());


#一周的第一天,英文下的星期幾
select now() as right_now,dayofweek(now()) as todays_day_of_week,dayname(now()) as todays_day_name;


#截取年和月份
select now() as right_now,extract(year from now()) as this_year,extract(month from now()) as this_month;


#相差月份
select months_between('2015-02-28','2015-01-28');


#查詢當前時間的季初日期
select now() as right_now,trunc(now(), 'Q') as current_quarter;


#兩周之后的季初時間
select now() + interval 2 weeks as 2_weeks_from_now,trunc(now() + interval 2 weeks,'Q') as still_current_quarter;


#一年中的第幾周
select now() as right_now,weekofyear(now()) as this_week;


#之前的兩周時間點
select now() as right_now,weeks_sub(now(), 2) as week_before_last;


#截取年份
select now() as right_now,year(now()) as this_year;


#增加一年
select now() as right_now,years_add(now(), 1) as next_year;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM