Oracle中日期运算实例

下面的例子列出常用的按照年、月、周进行计算的函数,假设当天为2016-02-03

SELECT
  today,                                        --当天
  add_months(today, -2) AS today_of_2month_ago, --上2个月的当天,跨年
  trunc(today, 'mm') AS first_day_month,        -- 2月的第一天
  last_day(today) AS last_day_month,            -- 2月的最后一天
  trunc(today, 'y') AS first_day_year,          -- 当年的第一天
  TRUNC(today, 'd') AS first_day_week,          -- 当天所在周的第一天
  next_day(today, 6) AS next_friday             -- 当天之后的第一个周五,从周日到周六按1-表示
FROM (
  SELECT to_date('2016-02-03', 'yyyy-mm-dd') AS today
  FROM dual
);

运行结果:

TODAY TODAY_OF_2MONTH_AGO FIRST_DAY_MONTH LAST_DAY_MONTH FIRST_DAY_YEAR FIRST_DAY_WEEK NEXT_FRIDAY
2016/2/3 2015/12/3 2016/2/1 2016/2/29 2016/1/1 2016/1/31 2016/2/5

 

Comments are closed.