【DataPM】Impala里的日期函數(shù)

最近Impala里查數(shù)據(jù)的時(shí)候,發(fā)現(xiàn)有很多時(shí)間函數(shù)不支持,因此,搜集整理了一批常用時(shí)間函數(shù),以備查詢。

讓日期自己與自己比較大小

datediff(first_value(create_time) over(partition by create_user order by create_time),create_time)

這是利用orcale里的窗口函數(shù),first_value () ?over(partition by ? ? order by ? )意思是先將creat_time按照create_user分組,再按create_time進(jìn)行排序,然后first_value()取出第一個(gè)時(shí)間,也就是最小時(shí)間,然后與create_time進(jìn)行比較,通過得到的數(shù)值比較大小。

有什么應(yīng)用場景呢?

若datediff()=0,那就表示只在當(dāng)天,可以用來判斷是否是只在當(dāng)天有數(shù)據(jù),也就是新增數(shù)據(jù)。

增加月份

add_months(timestamp date, int months)

add_months(timestamp date, bigint months)

Return type: timestamp

usage:add_months(now(),1)

增加日期

adddate(timestamp startdate, int days),

adddate(timestamp startdate, bigint days)

Return type: timestamp

usage:adddate(now(),1)

當(dāng)前時(shí)間戳

current_timestamp()和now()等價(jià)

日期相減

datediff(string enddate, string startdate)

Return type: int

usage:datediff("2018-08-05", "2018-08-03")

得到天,得到月份

day(string date)

Return type: int

usage: day("2018-08-05")

得到星期英文

dayname(string date)

Return type: string

usage:dayname("2018-08-05") Sunday

得到這一天是這周的第幾天

dayofweek(string date)? 1 (Sunday) to 7 (Saturday).

Return type: int

usage:dayofweek("2018-08-06")

加天數(shù)

days_add(timestamp startdate, int days)

Return type: timestamp

usage:days_add(now(),2)

減天數(shù)

days_sub(timestamp startdate, int days)

Return type: timestamp

usage:days_sub(now(), 2)

格式化日期

from_unixtime(bigint unixtime[, string format])

Return type: string

注意參數(shù)

usage:from_unixtime(1392394861,"yyyy-MM-dd");

得到小時(shí)

hour(string date)

Return type: int

usage:hour("2018-08-06 12:32:54")

增加小時(shí)

hours_add(timestamp date, int hours)

Return type: timestamp

usage:hours_add(now(),2)

減少

hours_sub(timestamp date, int hours)

Return type: timestamp

usage:hours_sub(now(),2)

得到分鐘

minute(string date)

Return type: int

usage:minute(now())

增加分鐘

minutes_add(timestamp date, int minutes)

Return type: timestamp

usage:minutes_add(now(),2)

減少分鐘

minutes_sub(timestamp date, int minutes)

Return type: timestamp

usage:minutes_sub(now(),2)

得到月份

month(string date)

Return type: int

usage:month("2018-08-06 12:32:54")

月份相加

months_add(timestamp date, int months)

Return type: timestamp

usage:months_add(now(),3)

減月份

months_sub(timestamp date, int months)

Return type: timestamp

months_sub(now(),3)

得到秒

second(string date)

Return type: int

秒加

seconds_add(timestamp date, int seconds)

Return type: timestamp

秒減

seconds_sub(timestamp date, int seconds)

Return type: timestamp

得到日期

to_date(now())

得到1970到今秒

unix_timestamp(),

unix_timestamp(string datetime),

unix_timestamp(string datetime, string format),

unix_timestamp(timestamp datetime)

Return type: bigint

得到這周是這年的多少周

weekofyear(string date)

Return type: int

usage:weekofyear("2018-08-06 12:32:54")

周加

weeks_add(timestamp date, int weeks)

Return type: timestamp

usage:weeks_add("2018-08-06 12:32:54", 1)

周減

weeks_sub(timestamp date, int weeks)

Return type: timestamp

usage:weeks_sub("2018-08-06 12:32:54", 1)

得到年

year(string date)

Return type: int

年加

years_add(timestamp date, int years)

Return type: timestamp

年減

years_sub(timestamp date, int years)

Return type: timestamp

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容