日期和时间函数
Palo支持的时间类型是TIMESTAMP,包括DATE和DATETIME两种类型。TIMESTAMP包含date和time两部分,日期和时间函数可以抽取出单个字段,如hour(), minute()。通常这些函数的返回值是整型。格式化日期的函数(如date_add())的返回值是字符串类型。用户可以通过加上或减去时间间隔来改变时间类型的值。时间间隔通常作为date_add()和date_sub()的第二个参数。Palo支持如下的日期和时间函数。
add_months(timestamp date, int months)
add_months(timestamp date, bigint months)
功能:返回指定date加上months个月的新date。和months_add()相同
返回类型:timestamp类型
举例:
如果这个月的这一日并不存在于目标月中,那么结果将是那个月的最后一天;如果参数中的months是负数,则是求先前的月。
- mysql> select now(), add_months(now(), 2);
- +---------------------+---------------------+
- | now() | add_months(now(), 2)|
- +---------------------+---------------------+
- | 2016-05-31 10:47:00 | 2016-07-31 10:47:00 |
- +---------------------+---------------------+
- 1 row in set (0.01 sec)
- mysql> select now(), add_months(now(), 1);
- +---------------------+---------------------+
- | now() | add_months(now(), 1)|
- +---------------------+---------------------+
- | 2016-05-31 10:47:14 | 2016-06-30 10:47:14 |
- +---------------------+---------------------+
- 1 row in set (0.01 sec)
- mysql> select now(), add_months(now(), -1);
- +---------------------+----------------------+
- | now() | add_months(now(), -1)|
- +---------------------+----------------------+
- | 2016-05-31 10:47:31 | 2016-04-30 10:47:31 |
- +---------------------+----------------------+
- 1 row in set (0.01 sec)
adddate(timestamp startdate, int days)
adddate(timestamp startdate, bigint days)
功能:给startdate加上指定的天数
返回类型:timestamp类型
举例:
- mysql> select adddate(date_column, 10) from big_table limit 1;
- +-------------------------------+
- | adddate(date_column, 10) |
- +-------------------------------+
- | 2014-01-11 00:00:00 |
- +-------------------------------+
current_timestamp()
功能:和now()函数功能相同,获取当前的时间
返回类型:timestamp类型
date_add(timestamp startdate, int days)
功能:给TIMESTAMP值加上指定的天数。第一个参数可以是字符串,如果字符串符合TIMESTAMP数据类型的格式,该字符串会自动转成TIMESTAMP类型。第二个参数是时间间隔。
返回类型:timestamp类型
date_format(timestamp day, string fmt)
功能:将日期类型按照format的类型转化为字符串,当前支持最大128字节的字符串,如果返回长度超过128,则返回NULL。
返回类型:string类型
format的含义如下:
- %a Abbreviated weekday name (Sun..Sat)
- %b Abbreviated month name (Jan..Dec)
- %c Month, numeric (0..12)
- %D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
- %d Day of the month, numeric (00..31)
- %e Day of the month, numeric (0..31)
- %f Microseconds (000000..999999)
- %H Hour (00..23)
- %h Hour (01..12)
- %I Hour (01..12)
- %i Minutes, numeric (00..59)
- %j Day of year (001..366)
- %k Hour (0..23)
- %l Hour (1..12)
- %M Month name (January..December)
- %m Month, numeric (00..12)
- %p AM or PM
- %r Time, 12-hour (hh:mm:ss followed by AM or PM)
- %S Seconds (00..59)
- %s Seconds (00..59)
- %T Time, 24-hour (hh:mm:ss)
- %U Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
- %u Week (00..53), where Monday is the first day of the week; WEEK() mode 1
- %V Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
- %v Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
- %W Weekday name (Sunday..Saturday)
- %w Day of the week (0=Sunday..6=Saturday)
- %X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
- %x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
- %Y Year, numeric, four digits
- %y Year, numeric (two digits)
- %% A literal “%” character
- %x x, for any “x” not listed above
举例:
- mysql> select date_format('2009-10-04 22:23:00', '%W %M %Y');
- +------------------------------------------------+
- | date_format('2009-10-04 22:23:00', '%W %M %Y') |
- +------------------------------------------------+
- | Sunday October 2009 |
- +------------------------------------------------+
- 1 row in set (0.01 sec)
- mysql> select date_format('2007-10-04 22:23:00', '%H:%i:%s');
- +------------------------------------------------+
- | date_format('2007-10-04 22:23:00', '%H:%i:%s') |
- +------------------------------------------------+
- | 22:23:00 |
- +------------------------------------------------+
- 1 row in set (0.01 sec)
- mysql> select date_format('1900-10-04 22:23:00', '%D %y %a %d %m %b %j');
- +------------------------------------------------------------+
- | date_format('1900-10-04 22:23:00', '%D %y %a %d %m %b %j') |
- +------------------------------------------------------------+
- | 4th 00 Thu 04 10 Oct 277 |
- +------------------------------------------------------------+
- 1 row in set (0.03 sec)
- mysql> select date_format('1997-10-04 22:23:00', '%H %k %I %r %T %S %w');
- +------------------------------------------------------------+
- | date_format('1997-10-04 22:23:00', '%H %k %I %r %T %S %w') |
- +------------------------------------------------------------+
- | 22 22 10 10:23:00 PM 22:23:00 00 6 |
- +------------------------------------------------------------+
- 1 row in set (0.00 sec)
date_sub(timestamp startdate, int days)
功能:给TIMESTAMP值减去指定的天数。第一个参数可以是字符串,如果字符串符合TIMESTAMP数据类型的格式,该字符串会自动转成成TIMESTAMP类型。第二个参数是时间间隔。
返回类型:timestamp类型
datediff(string enddate, string startdate)
功能:返回两个日期的天数差值
返回类型:int类型
day(string date)
dayofmonth(string date)
功能:返回日期中的天字段
返回类型:int类型
举例:
- mysql> select dayofmonth('2013-01-21');
- +-----------------------------------+
- | dayofmonth('2013-01-21 00:00:00') |
- +-----------------------------------+
- | 21 |
- +-----------------------------------+
- 1 row in set (0.01 sec)
days_add(timestamp startdate, int days)
days_add(timestamp startdate, bigint days)
功能:给startdate加上指定的天数,和date_add函数相似,差别在于本函数的参数是TIMESTAMP类型而不是string类型。
返回类型:timestamp类型
days_sub(timestamp startdate, int days)
days_sub(timestamp startdate, bigint days)
功能:给startdate减去指定的天数,和date_dub函数相似,差别在于本函数的参数是TIMESTAMP类型而不是string类型。
返回类型:timestamp类型
extract(unit FROM timestamp)
功能:提取timestamp某个指定单位的值。单位可以为year, month, day, hour, minute或者second
返回类型:int类型
举例:
- mysql> select now() as right_now,
- -> extract(year from now()) as this_year,
- -> extract(month from now()) as this_month;
- +---------------------+-----------+------------+
- | right_now | this_year | this_month |
- +---------------------+-----------+------------+
- | 2017-10-16 20:47:28 | 2017 | 10 |
- +---------------------+-----------+------------+
- 1 row in set (0.01 sec)
- mysql> select now() as right_now,
- -> extract(day from now()) as this_day,
- -> extract(hour from now()) as this_hour;
- +---------------------+----------+-----------+
- | right_now | this_day | this_hour |
- +---------------------+----------+-----------+
- | 2017-10-16 20:47:34 | 16 | 20 |
- +---------------------+----------+-----------+
- 1 row in set (0.01 sec)
from_unixtime(bigint unixtime[, string format])
功能:将unix时间(自1970年1月1日起经过的秒数)转换成相应格式的日期类型
返回类型:字符串类型
使用说明:当前日期格式是大小写敏感的,用户尤其要区分小写m(表达分钟)和大写M(表达月份)。日期字符串的完整型式是"yyyy-MM-dd HHss.SSSSSS",也可以只包含其中部分字段。
举例:
- mysql> select from_unixtime(100000);
- +-----------------------+
- | from_unixtime(100000) |
- +-----------------------+
- | 1970-01-02 11:46:40 |
- +-----------------------+
- 1 row in set (0.01 sec)
- mysql> select from_unixtime(100000, 'yyyy-MM-dd');
- +-------------------------------------+
- | from_unixtime(100000, 'yyyy-MM-dd') |
- +-------------------------------------+
- | 1970-01-02 |
- +-------------------------------------+
- 1 row in set (0.00 sec)
- mysql> select from_unixtime(1392394861, 'yyyy-MM-dd');
- +-----------------------------------------+
- | from_unixtime(1392394861, 'yyyy-MM-dd') |
- +-----------------------------------------+
- | 2014-02-15 |
- +-----------------------------------------+
- 1 row in set (0.00 sec)
unix_timestamp()和from_unixtime()经常结合使用,将时间戳类型转换成指定格式的字符串。
- mysql> select from_unixtime(unix_timestamp(now()), 'yyyy-MM-dd');
- +----------------------------------------------------+
- | from_unixtime(unix_timestamp(now()), 'yyyy-MM-dd') |
- +----------------------------------------------------+
- | 2014-01-01 |
- +----------------------------------------------------+
hour(string date)
功能:返回字符串所表达日期的小时字段
返回类型:int类型
hours_add(timestamp date, int hours)
hours_add(timestamp date, bigint hours)
功能:返回指定的日期加上若干小时后的时间
返回类型:timestamp
hours_sub(timestamp date, int hours)
hours_sub(timestamp date, bigint hours)
功能:返回指定的日期减去若干小时后的时间
返回类型:timestamp
microseconds_add(timestamp date, int microseconds)
microseconds_add(timestamp date, bigint microseconds)
功能:返回指定的日期加上若干微秒后的时间
返回类型:timestamp
microseconds_sub(timestamp date, int microseconds)
microseconds_sub(timestamp date, bigint microseconds)
功能:返回指定的日期减去若干微秒后的时间
返回类型:timestamp
minute(string date)
功能:返回字符串所表达日期的分钟字段
返回类型:int类型
minutes_add(timestamp date, int minutes)
minutes_add(timestamp date, bigint minutes)
功能:返回指定的日期加上若干分钟后的时间
返回类型:timestamp
minutes_sub(timestamp date, int minutes)
minutes_sub(timestamp date, bigint minutes)
功能:返回指定的日期减去若干分钟后的时间
返回类型:timestamp
month(string date)
功能:返回字符串所表达的日期的月份字段
返回类型:int类型
months_add(timestamp date, int months)
months_add(timestamp date, bigint months)
功能:返回指定的日期加上若干月份后的时间
返回类型:timestamp
months_sub(timestamp date, int months)
months_sub(timestamp date, bigint months)
功能:返回指定的日期减去若干月份后的时间
返回类型:timestamp
now()
功能:返回当前的日期和时间(东八区的时区)
返回类型:timestamp
second(string date)
功能:返回字符串所表达的日期的秒字段
返回类型:int 类型
seconds_add(timestamp date, int seconds)
seconds_add(timestamp date, bigint seconds)
功能:返回指定的日期加上若干秒后的时间
返回类型:timestamp
seconds_sub(timestamp date, int seconds)
seconds_sub(timestamp date, bigint seconds)
功能:返回指定的日期减去若干秒后的时间
返回类型:timestamp
subdate(timestamp startdate, int days)
subdate(timestamp startdate, bigint days)
功能:从startdate的时间减去若干天后的时间。和date_sub()函数相似,但是本函数的第一个参数是确切的TIMESTAMP,而非可以转成TIMESTAMP类型的字符串。
返回类型:timestamp
str_to_date(string str, string format)
功能:通过format指定的方式将str转化为timestamp类型,如果转化结果不对返回NULL。支持的format格式与date_format一致。
返回类型:timestamp
to_date(timestamp)
功能:返回timestamp的date域
返回类型:string类型
举例:
- mysql> select now() as right_now,
- -> concat('The date today is ',to_date(now()),'.') as date_announcement;
- +---------------------+-------------------------------+
- | right_now | date_announcement |
- +---------------------+-------------------------------+
- | 2017-10-16 21:10:24 | The date today is 2017-10-16. |
- +---------------------+-------------------------------+
- 1 row in set (0.01 sec)
unix_timestamp()
unix_timestamp(string datetime)
unix_timestamp(string datetime, string format)
unix_timestamp(timestamp datetime)
功能:返回当前时间的时间戳(相对1970年1月1日的秒数)或者从一个指定的日期和时间转换成时间戳。返回的时间戳是相对于格林尼治时区的时间戳。
返回类型:bigint类型
weeks_add(timestamp date, int weeks)
weeks_add(timestamp date, bigint weeks)
功能:返回指定的日期加上若干周后的时间
返回类型:timestamp
weeksofyear(timestamp date)
功能:获得一年中的第几周
返回类型:int
weeks_add(timestamp date, int weeks)
weeks_add(timestamp date, bigint weeks)
功能:返回指定的日期加上若干周后的时间
返回类型:timestamp
weeks_sub(timestamp date, int weeks)
weeks_sub(timestamp date, bigint weeks)
功能:返回指定的日期减去若干周后的时间
返回类型:timestamp
year(string date)
功能:返回字符串所表达的日期的年字段
返回类型:int类型
years_add(timestamp date, int years)
years_add(timestamp date, bigint years)
功能:返回指定的日期加上若干年后的时间
返回类型:timestamp
years_sub(timestamp date, int years)
years_sub(timestamp date, bigint years)
功能:返回指定的日期减去若干年的时间
返回类型:timestamp