Oracle 时间操控

  Oracle 时间操控

说明:主要是收集关于日常工作常用oracle时间操作记录汇总。

1.   截取系统时间

select extract(year/month/day/hour from sysdate/systimestamp) from dual;

-3天的月份sql: select extract(month from sysdate-3) from dual;

求当月的最后一天日期: select extract(day from last_day(sysdate)) from dual;

2个时间的月份差距:

   select 'true' from dual where   months_between(to_date(to_char(sysdate,'yyyyMMdd HH24:mi:ss'),

                'yyyyMMdd HH24:mi:ss'),to_date('20090820121035','yyyyMMdd HH24:mi:ss'))>1 ;

2.   提取系统时间

 -”后的格式说明:2 12/24 半天 1440/1440 一天

select TRUNC(SYSDATE)-2+12/24+1440/1440 from dual;

3.   其他系统时间说明

Here are a couple of examples: Description Date Expression Now SYSDATE

Tomorow/ next day = SYSDATE + 1

Seven days from now = SYSDATE + 7

One hour from now = SYSDATE + 1/24

Three hours from now = SYSDATE + 3/24

An half hour from now = SYSDATE + 1/48

10 minutes from now = SYSDATE + 10/1440

30 seconds from now = SYSDATE + 30/86400

Tomorrow at 12 midnight = TRUNC(SYSDATE + 1)

Tomorrow at 8 AM = TRUNC(SYSDATE + 1) + 8/24

Next Monday at 12:00 noon = NEXT_DAY(TRUNC(SYSDATE), 'MONDAY') + 12/24

First day of next month at 12 midnight = TRUNC(LAST_DAY(SYSDATE ) + 1)

    First day of the current month= TRUNC(LAST_DAY(ADD_MONTHS(SYSDATE,-1))) + 1

The next Monday, Wednesday or Friday at 9 a.m =

        TRUNC(LEAST(NEXT_DAY(sysdate,MONDAY ),

            NEXT_DAY(sysdate,WEDNESDAY), NEXT_DAY(sysdate,FRIDAY ))) + (9/24)

4.   各种时间提取

--取得当前日期是本月的第几周

select to_char(sysdate,'YYYYMMDD W HH24:MI:SS') from dual;

select to_char(sysdate,'W') from dual;

--取得当前日期是一个星期中的第几天,注重星期日是第一天

select sysdate,to_char(sysdate,'D') from dual;

--类似:

select to_char(sysdate,'yyyy') from dual; --

select to_char(sysdate,'Q') from dual; --

select to_char(sysdate,'mm') from dual; --

select to_char(sysdate,'dd') from dual; --

/*ddd 年中的第几天

WW 年中的第几个星期

W 该月中第几个星期

D 周中的星期几

hh 小时(12)

hh24 小时(24)

Mi

ss */

 

--取当前日期是星期几中文显示:

select to_char(sysdate,'day') from dual;

--假如一个表在一个date类型的字段上面建立了索引,如何使用

alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'

--得到当前的日期

select sysdate from dual;

--得到当天凌晨000秒的日期

select trunc(sysdate) from dual;

-- 得到这天的最后一秒

select trunc(sysdate) + 0.99999 from dual;

-- 得到小时的具体数值

select trunc(sysdate) + 1/24 from dual;

select trunc(sysdate) + 7/24 from dual;

--得到明天凌晨000秒的日期

select trunc(sysdate+1) from dual;

--本月一日的日期

select trunc(sysdate,'mm') from dual;

--得到下月一日的日期

select trunc(add_months(sysdate,1),'mm') from dual;

--返回当前月的最后一天?

select last_day(sysdate) from dual;       

select last_day(trunc(sysdate)) from dual;

select trunc(last_day(sysdate)) from dual;

select trunc(add_months(sysdate,1),'mm') - 1 from dual;

--得到一年的每一天

select trunc(sysdate, 'yyyy') + rn - 1 date0

  from (select rownum rn from all_objects where rownum < 366);

--今天是今年的第N

SELECT TO_CHAR(SYSDATE,'DDD') FROM DUAL;

--如何在给现有的日期加上2

select add_months(sysdate,24) from dual;

--判定某一日子所在年分是否为润年

select decode(to_char(last_day(trunc(sysdate,'y')+31),'dd'),'29','闰年','平年') from dual;

--判定两年后是否为润年

select decode(to_char(last_day(trunc(add_months(sysdate,24),'y')+31),'dd'),'29','闰年','平年') from dual;

--得到日期的季度

select ceil(to_number(to_char(sysdate,'mm'))/3) from dual;

select to_char(sysdate, 'Q') from dual;

你可能感兴趣的:(oracle,sql,Date,工作,session)