This article brings you relevant knowledge about Oracle, which mainly introduces some commonly used date function-related issues, including SYSDATE, ADD_MONTHS, LAST_DAY, TRUNC, ROUND, etc. I hope Helpful to everyone.
Recommended tutorial: "Oracle Tutorial"
This function has no parameters and can get the current time of the system.
Case code:
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
Result:
select systimestamp from dual;Result: Database time zone function
select dbtimezone from dual;Result:
## Add the specified month function to the date:
ADD_MONTHS(r,n) functionr: the specified date.
n: The number of months to be added. If N is a negative number, it means the number of months to be subtracted.
Case code:
select to_char(add_months(to_date('2018-11-12', 'yyyy-mm-dd'), 1), 'yyyy-mm-dd'), to_char(add_months(to_date('2018-10-31', 'yyyy-mm-dd'), 1), 'yyyy-mm-dd'), to_char(add_months(to_date('2018-09-30', 'yyyy-mm-dd'), 1), 'yyyy-mm-dd') from dual;
Result: (If the specified date is the last day of the month, the returned last day of the new month is also the last day of the new month. If the new month is less than the specified month date, The valid date will be automatically recalled)
Last day of month function
LAST_DAY(r) functionCase code:
select last_day(sysdate) from dual;
Result:
Date function one week after the specified date:
NEXT_DAY(r,c) functionCase code:
select next_day(to_date('2018-11-12','yyyy-mm-dd'),'星期四') from dual;
Result:
Function that returns a specific part of the specified date
EXTRACT(time) functionCase code:
select extract(year from timestamp '2018-11-12 15:36:01') as year, extract(month from timestamp '2018-11-12 15:36:01') as month, extract(day from timestamp '2018-11-12 15:36:01') as day, extract(minute from timestamp '2018-11-12 15:36:01') as minute, extract(second from timestamp '2018-11-12 15:36:01') as second from dual;
Result:
Return the number of months between two dates:
MONTHS_BETWEEN(r1,r2) functionCase code:
select months_between(to_date('2018-11-12', 'yyyy-mm-dd'), to_date('2017-11-12', 'yyyy-mm-dd')) as zs, --整数 months_between(to_date('2018-11-12', 'yyyy-mm-dd'), to_date('2017-10-11', 'yyyy-mm-dd')) as xs, --小数 months_between(to_date('2017-11-12', 'yyyy-mm-dd'), to_date('2018-10-12', 'yyyy-mm-dd')) as fs --负数 from dual;
Result:
Date interception function
ROUND(r[, f]) FunctionCase code:
select sysdate, --当前时间 round(sysdate, 'yyyy') as year, --按年 round(sysdate, 'mm') as month, --按月 round(sysdate, 'dd') as day, --按天 round(sysdate) as mr_day, --默认不填按天 round(sysdate, 'hh24') as hour --按小时 from dual;
Result:
##TRUNC(r[,f]) function
select sysdate, --当前时间 trunc(sysdate, 'yyyy') as year, --按年 trunc(sysdate, 'mm') as month, --按月 trunc(sysdate, 'dd') as day, --按天 trunc(sysdate) as mr_day, --默认不填按天 trunc(sysdate, 'hh24') as hour --按小时 from dual;Result:
Recommended tutorial: "Oracle Learning Tutorial
"The above is the detailed content of Summary and sharing of Oracle date functions. For more information, please follow other related articles on the PHP Chinese website!