SELECT to_date('20200401','yyyymmdd');
> 2020-04-01 00:00:00
select TO_CHAR(TO_DATE('20200401','yyyymmdd'),'yyyy-mm-dd');
>2020-04-01
select TO_CHAR('2020-04-01 20:20:20','yyyy-mm-dd');
>2020-04-01
select TO_CHAR('2020-04-01 20:20:20','yyyymmdd');
>20200401
select TO_CHAR('2020-04-01 20:20:20','yyyy-mm-dd hh');
>2020-04-01 20
select TO_CHAR('2020-04-01 20:20:20','yyyymmddhh');
>2020040120
select TO_CHAR('2020-04-01 20:20:20','hh');
>20
select datepart('2020-04-01 20:20:20','yyyy');
>2020
select datepart('2020-04-01 20:20:20','mm');
>4
select datepart('2020-04-01 20:20:20','mi');
>20
## 日期转化为时间戳 ##
select unix_timestamp('2020-04-01 20:20:20');
> 1585743620
select unix_timestamp('2020-04-01 00:00:00');
>1585670400
SELECT unix_timestamp(to_date('20200401','yyyymmdd'));
>1585670400
select from_unixtime (1585743620)
>2020-04-01 20:20:20
SELECT DATEADD('2020-04-01 20:20:20',1,'dd');
>2020-04-02 20:20:20
SELECT DATEADD('2020-04-01 20:20:20',1,'dd');
>2020-03-31 20:20:20
对年修改 yyyy
对月修改 mm
对天修改 dd
对小时修改 hh
对分钟修改 mi
对秒修改 ss
命令格式:
datediff(endtime, starttime, datepart)
用途:
计算两个时间的差值,并转换成指定的单位,如:秒。
若start = ‘2005-12-31 23:59:59’, end = ‘2006-01-01 00:00:00’:
select datediff(end, start, 'dd') = 1
select datediff(end, start, 'mm') = 1
select datediff(end, start, 'yyyy') = 1
select datediff(end, start, 'hh') = 1
select datediff(end, start, 'mi') = 1
select datediff(end, start, 'ss') = 1
select datetrunc("2011-12-07 16:28:46", "yyyy")
> "2011-01-01 00:00:00"
select datetrunc("2011-12-07 16:28:46", "month")
>"2011-12-01 00:00:00"
select datetrunc("2011-12-07 16:28:46", "dd")
> "2011-12-07 00:00:00"
select datetrunc("2011-12-07 16:28:46", "hh");
>2011-12-07 16:00:00
select datetrunc("2011-12-07 16:28:46", "mi");
>2011-12-07 16:28
select datetrunc("2011-12-07 16:28:46", "ss");
>2011-12-07 16:28:46