读书人

Oracle惯用日期操作

发布时间: 2012-11-23 00:03:29 作者: rapoo

Oracle常用日期操作


?--Oracle trunc()函数的用法

/**************日期********************/

1.select trunc(sysdate) from dual ?--2011-3-18 ?今天的日期为2011-3-18

2.select trunc(sysdate, 'mm') ? from ? dual ?--2011-3-1 ? ?返回当月第一天.

3.select trunc(sysdate,'yy') from dual ?--2011-1-1 ? ? ? 返回当年第一天

4.select trunc(sysdate,'dd') from dual ?--2011-3-18 ? ?返回当前年月日

5.select trunc(sysdate,'yyyy') from dual ?--2011-1-1 ? 返回当年第一天

6.select trunc(sysdate,'d') from dual ?--2011-3-13 (星期天)返回当前星期的第一天

7.select trunc(sysdate, 'hh') from dual ? --2011-3-18 14:00:00 ? 当前时间为14:41 ??

8.select trunc(sysdate, 'mi') from dual ?--2011-3-18 14:41:00 ? TRUNC()函数没有秒的精确

/***************数字********************/

/*

TRUNC(number,num_digits)?

Number 需要截尾取整的数字。?

Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。

TRUNC()函数截取时不进行四舍五入

*/

9.select trunc(123.458) from dual --123

10.select trunc(123.458,0) from dual --123

11.select trunc(123.458,1) from dual --123.4

12.select trunc(123.458,-1) from dual --120

13.select trunc(123.458,-4) from dual --0

14.select trunc(123.458,4) from dual ?--123.458

15.select trunc(123) from dual ?--123

16.select trunc(123,1) from dual --123

17.select trunc(123,-1) from dual --120



№1:取得当前日期是本月的第几周



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

? ??

? TO_CHAR(SYSDATE,'YY ??

? ------------------- ??

? 20030327 ? 4 ? 18:16:09 ??

? ??

? SQL> ? select ? to_char(sysdate,'W') ? from ? dual; ??

? ??

? T ??

? - ??

? 4?

№2:取得当前日期是一个星期中的第几天,注意星期日是第一天


? SQL> ? select ? sysdate,to_char(sysdate,'D') ? from ? dual; ??

? ??

? SYSDATE ? ? ? T ??

? --------- ? - ??

? 27-MAR-03 ? 5 ?

类似:


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 ? 秒

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


SQL> ? select ? to_char(sysdate,'day') ? from ? dual; ??

? ??

? TO_CHAR(SYSDATE,'DAY') ??

? ---------------------- ??

? 星期四 ?

№4:如果一个表在一个date类型的字段上面建立了索引,如何使用

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



№5: 得到当前的日期

select sysdate from dual;

№6: 得到当天凌晨0点0分0秒的日期

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;


№7:得到明天凌晨0点0分0秒的日期

select trunc(sysdate+1) from dual;


select trunc(sysdate)+1 from dual;


№8: 本月一日的日期

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


№9:得到下月一日的日期

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


№10:返回当前月的最后一天?

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;



№11: 得到一年的每一天

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

from?

(select rownum rn from all_objects?

where rownum<366);



№12:今天是今年的第N天

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


№13:如何在给现有的日期加上2年

select add_months(sysdate,24) from dual;


№14:判断某一日子所在年分是否为润年?

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

№15:判断两年后是否为润年

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



№16:得到日期的季度


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

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

读书人网 >其他数据库

热点推荐