获取当前日期
now()
select now()
rst:2019-10-24 10:58:47.128771000
current_timestamp()
select current_timestamp()
rst:2019-10-24 11:29:43.718155000
now()和current_timestamp()等价,都是获取当前系统时间
unix_timestamp()
select unix_timestamp()
rst:1571886216
日期转换操作
转换成date
SELECT to_date(now());
rst:2019-10-24
SELECT to_date('2019-07-04');
rst:2019-07-04
字符串转成timestamp格式
select cast('1966-07-30' as timestamp);
rst:1966-07-30 00:00:00
select cast('1985-09-25 17:45:30.005' as timestamp);
rst:1985-09-25 17:45:30.005000000
select cast('08:30:00' as timestamp);
rst:08:30:00
日期的加减
- 当前日期加7天
SELECT days_add(now(),7);
rst:2019-10-31 11:38:50.071445000
- 当前日期减一个月
SELECT months_add(now(),-1);
rst:2019-09-24 17:19:13.609754000
unixtime转成yyyymmdd格式
SELECT from_unixtime(unix_timestamp(),'yyyyMMdd')
rst:20191024
使用from_unixtime转换时yyyyMMdd中的月份要大写,否则不能正确转换月份,错误如下:
SELECT from_unixtime(unix_timestamp(),'yyyymmdd')
rst:20192724
日期转字符串
- 当前日期加7天并转换成yyyymmdd形式
SELECT from_unixtime(unix_timestamp(days_add(now(),7)),'yyyyMMdd');
rst:20191031
字符串转日期
- 将字符串日期转成yyyymmdd形式
SELECT from_unixtime(unix_timestamp(days_add(cast('2019-07-20' as TIMESTAMP),7)),'yyyyMMdd');
rst:20190727
* 注意:传入的日期格式需带'-',不然结果会是null,例如:
SELECT from_unixtime(unix_timestamp(days_add(cast('20190720' as TIMESTAMP),7)),'yyyyMMdd');
rst:NULL