Impala is different from hive. Hive is a weak type. For example, int and string can be compared under most conditions.
For example, hive select 1='1' --the result is true or false
But impala select 1='1' reports an error operands of type TINYINT and STRING are not comparable: 1 = '1'
The advantage of this is that type-consistent results are more accurate, but the disadvantage is that it increases the developer's energy. .
Without further ado, let’s get straight to the case.
How to check what functions impala has?
use _impala_builtins;
show functions like "*date*";
1.NOW() and current_timestamp() return the current time, and the return value is timestamp type
select current_timestamp(),now()
2.current_date() returns the current year, month and day, and the return value is string type
select current_date()
3.unix_timestamp() returns the current timestamp, and the return value is bigInt type
unix_timestamp()
unix_timestamp(STRING)
unix_timestamp(STRING, STRING)
unix_timestamp(TIMESTAMP)
select 'unix_timestamp()',unix_timestamp() union all
select 'unix_timestamp(NOW())' ,unix_timestamp(NOW())union all
select 'unix_timestamp(\'2023-01-01\')',unix_timestamp('2023-01-01')union all
select 'unix_timestamp(\'20230101\',\'yyyyMMdd\')',unix_timestamp('20230101','yyyyMMdd') union all
select 'unix_timestamp(\'2023-01-01 00:00:01\')',unix_timestamp('2023-01-01 00:00:01')
4.from_timestamp and from_unixtime return string types.
Note that this impala does not have date_format or to_date rows. For the standard time format yyyy-MM-dd, you need these two functions if you want to format it into the type you need.
from_timestamp(TIMESTAMP, STRING)
from_unixtime(BIGINT)
from_unixtime(BIGINT, STRING)
from_unixtime(INT)
from_unixtime(INT, STRING)
select 'from_timestamp(NOW(),\'yyyy-MM-dd HH:mm:ss\') ',from_timestamp(NOW(),'yyyy-MM-dd HH:mm:ss') union all
select 'from_unixtime(cast (1695125510 as bigint)) ',from_unixtime(cast (1695125510 as bigint)) union all
select 'from_unixtime(cast (1695125510 as bigint),\'yyyy-MM\') ',from_unixtime(cast (1695125510 as bigint),'yyyy-MM') union all
select 'from_unixtime(1695125510) ',from_unixtime(1695125510) union all
select 'from_unixtime(1695125510,\'yyyy-MM-dd \')',from_unixtime(1695125510,'yyyy-MM-dd ')
So 2 and 3 are often used together
demo1 20231122 converted to 2023-11-22
select "from_unixtime(unix_timestamp('20221122','yyyyMMdd'),'yyyy-MM-dd')", from_unixtime(unix_timestamp('20221122','yyyyMMdd'),'yyyy-MM-dd')
demo2 obtains various formats of the current time
select "from_unixtime(unix_timestamp(),'yyyy-MM-dd')", from_unixtime(unix_timestamp(),'yyyy-MM-dd') union all
select "from_unixtime(unix_timestamp(),'yyyyMMddHHmmss')", from_unixtime(unix_timestamp(),'yyyyMMddHHmmss')
Some people may ask why not use select now() directly, because now() returns tmiestamp, and from_unixtime is string. Generally speaking, we prefer to use string.
5 add_months operation, the input parameter is date, timestamp can also be string, and the return value is timestamp
add_months(DATE, BIGINT)
add_months(DATE, INT)
add_months(TIMESTAMP, BIGINT)
add_months(TIMESTAMP, INT)
months_add(DATE, BIGINT)
months_add(DATE, INT)
months_add(TIMESTAMP, BIGINT)
months_add(TIMESTAMP, INT)
The second parameter int is the same as bigint. There is no difference. It is an overloading of a method, supporting different types of parameters.
select "add_months(NOW() ,0)",add_months(NOW() ,0) union all
select "add_months('2022-01-23',1)",add_months('2022-01-23',1) union all
select "add_months(NOW() ,cast(1 as bigint ))",add_months(NOW() ,cast(1 as bigint ))
6 adddate and date_add, input parameters date and timestamp, the return value is said to have date and timestamp, but actually both are timestamp
increase in days
adddate(DATE, BIGINT)
adddate(DATE, INT)
adddate(TIMESTAMP, BIGINT)
adddate(TIMESTAMP, INT)
date_add(DATE, BIGINT)
date_add(DATE, INT)
date_add(TIMESTAMP, BIGINT)
date_add(TIMESTAMP, INT)
days_add(DATE, BIGINT)
days_add(DATE, INT)
days_add(TIMESTAMP, BIGINT)
days_add(TIMESTAMP, INT)
decrease in days
date_sub(DATE, BIGINT)
date_sub(DATE, INT)
date_sub(TIMESTAMP, BIGINT)
date_sub(TIMESTAMP, INT)
subdate(DATE, BIGINT)
subdate(DATE, INT)
subdate(TIMESTAMP, BIGINT)
subdate(TIMESTAMP, INT)
In fact, the operation is the same as add_months
select 'adddate(now(),1)',adddate(now(),1) union all
select 'adddate(now(),-1)',adddate(now(),-1) union all
select 'date_add(now(),0)',date_add(now(),0) union all
select 'date_add(CURRENT_DATE(),1)',date_add(CURRENT_DATE(),1) union all
select 'date_add(current_date(),-1)',date_add(current_date(),-1)
7. In the same way, the calculation of time parameters will not be given one by one.
increase in year
weeks_add(DATE, BIGINT)
weeks_add(DATE, INT)
weeks_add(TIMESTAMP, BIGINT)
weeks_add(TIMESTAMP, INT)
周的增加
years_add(DATE, BIGINT)
years_add(DATE, INT)
years_add(TIMESTAMP, BIGINT)
years_add(TIMESTAMP, INT)
increase in hours
hours_add(TIMESTAMP, BIGINT)
hours_add(TIMESTAMP, INT)
minutes increase
minutes_add(TIMESTAMP, BIGINT)
minutes_add(TIMESTAMP, INT)
millisecond increase
milliseconds_add(TIMESTAMP, BIGINT)
milliseconds_add(TIMESTAMP, INT)
Microsecond increase
microseconds_add(TIMESTAMP, BIGINT)
microseconds_add(TIMESTAMP, INT)
8 date_trunc() rounds timestamp, and the return type is timestamp
date_trunc(STRING, DATE)
date_trunc(STRING, TIMESTAMP)
select "date_trunc('year', CURRENT_TIMESTAMP()) ",date_trunc('year', CURRENT_TIMESTAMP()) union all
select "date_trunc('month', CURRENT_TIMESTAMP()) ",date_trunc('month', CURRENT_TIMESTAMP()) union all
select "date_trunc('week', CURRENT_TIMESTAMP()) ",date_trunc('week', CURRENT_TIMESTAMP()) union all
select "date_trunc('day', CURRENT_TIMESTAMP()) ",date_trunc('day', CURRENT_TIMESTAMP()) union all
select "date_trunc('hour', CURRENT_TIMESTAMP()) ",date_trunc('hour', CURRENT_TIMESTAMP()) union all
select "date_trunc('minute', CURRENT_TIMESTAMP())",date_trunc('minute', CURRENT_TIMESTAMP())union all
select "date_trunc('second', CURRENT_TIMESTAMP())",date_trunc('second', CURRENT_TIMESTAMP())
9 datediff finds the interval between dates. The return value is int and can be negative.
datediff(DATE, DATE)
datediff(TIMESTAMP, TIMESTAMP)
select "datediff('2023-01-04',now()) ",datediff('2023-01-04',now()) union all
select "datediff('2023-01-04',CURRENT_DATE())",datediff('2023-01-04',CURRENT_DATE())union all
select "datediff('2023-01-03','2023-01-01') ",datediff('2023-01-03','2023-01-01')
10 to_date normalizes timestamp to year, month and day. The input parameter can be string or timestamp and the return value is timestamp.
to_date(TIMESTAMP)
In fact, you can also use to_date(STRING)
select "to_date(now()) ",to_date(now()) union all
select "to_date('2023-1-2') ",to_date('2023-1-2') union all
select "to_date('2022-01-01')",to_date('2022-01-01')union all
select "to_date('2022/01/01')",to_date('2022/01/01')
11 Get a separate function of year, day, hour, minute and second. Of course, you can also get
date_part(STRING, DATE)
date_part(STRING, TIMESTAMP) through formatting.
select "date_part('year',now()) ",date_part('year',now()) union all
select "date_part('month',now()) ",date_part('month',now()) union all
select "date_part('day',now()) ",date_part('day',now()) union all
select "date_part('hour',now()) ",date_part('hour',now()) union all
select "date_part('minute',now())",date_part('minute',now())union all
select "date_part('second',now())",date_part('second',now())union all
select "YEAR(now()) ",YEAR(now()) union all
select "MONTH(now()) ",MONTH(now()) union all
select "day(now()) ",day(now()) union all
select "HOUR(now()) ",HOUR(now()) union all
select "MINUTE(now()) ",MINUTE(now()) union all
select "SECOND (now()) ",SECOND (now())
11 date_cmp This function compares the size of two dates. It can be regarded as a simplified version of datediff, and the return value is int.
1 means the first date is greater than the second date.
date_cmp(DATE, DATE)
select "date_cmp('2023-09-22','2023-09-21')",date_cmp('2023-09-22','2023-09-21') union all
select "date_cmp('2023-09-22','2023-09-22')",date_cmp('2023-09-22','2023-09-22') union all
select "date_cmp('2023-09-22','2023-09-23')",date_cmp('2023-09-22','2023-09-23')
12 Determine what day the current date is this year and what day it is this week
dayofmonth(DATE)
dayofmonth(TIMESTAMP)
dayofweek(DATE)
dayofweek(TIMESTAMP)
dayofyear(DATE)
dayofyear(TIMESTAMP)
select "DAYOFMONTH(CURRENT_DATE())",DAYOFMONTH(CURRENT_DATE()) union all
select "DAYOFWEEK(CURRENT_DATE()) ",DAYOFWEEK(CURRENT_DATE()) union all
select "DAYOFYEAR(CURRENT_DATE()) ",DAYOFYEAR(CURRENT_DATE())Note that dayofweek here is not Saturday . Why is the current time 2023-09-22 shown as Saturday? It should be that foreigners regard Sunday as the first day, so Friday is the sixth day.