Oracle时间日期操作方法小结第1/2页

sysdate+(5/24/60/60) 在系统时间基础上延迟5秒

sysdate+5/24/60 在系统时间基础上延迟5分钟

sysdate+5/24 在系统时间基础上延迟5小时

sysdate+5 在系统时间基础上延迟5天

add_months(sysdate,-5) 在系统时间基础上延迟5月

add_months(sysdate,-5*12) 在系统时间基础上延迟5年

上月末的日期:select last_day(add_months(sysdate, -1)) from dual;

本月的最后一秒:select trunc(add_months(sysdate,1),’mm’) – 1/24/60/60 from dual

本周星期一的日期:select trunc(sysdate,’day’)+1 from dual

年初至今的天数:select ceil(sysdate – trunc(sysdate, ‘year’)) from dual;

今天是今年的第几周 :select to_char(sysdate,’fmww’) from dual

今天是本月的第几周:select to_char(sysdate,’ww’) – to_char(trunc(sysdate,’mm’),’ww’) + 1 as “weekofmon” from dual

本月的天数

select to_char(last_day(sysdate),’dd’) days from dual

今年的天数

select add_months(trunc(sysdate,’year’), 12) – trunc(sysdate,’year’) from dual

下个星期一的日期

select next_day(sysdate,’monday’) from dual

============================================

–计算工作日方法

create table t(s date,e date);

alter session set nls_date_format = ‘yyyy-mm-dd’;

insert into t values(‘2003-03-01′,’2003-03-03’);

insert into t values(‘2003-03-02′,’2003-03-03’);

insert into t values(‘2003-03-07′,’2003-03-08’);

insert into t values(‘2003-03-07′,’2003-03-09’);

insert into t values(‘2003-03-05′,’2003-03-07’);

insert into t values(‘2003-02-01′,’2003-03-31’);

— 这里假定日期都是不带时间的,否则在所有日期前加trunc即可。

select s,e,e-s+1 total_days,

trunc((e-s+1)/7)*5 + length(replace(substr(‘01111100111110′,to_char(s,’d’),mod(e-s+1,7)),’0′,”)) work_days

from t;

— drop table t;

引此:http://www.itpub.net/showthread.php?s=1635506cd5f48b1bc3adbe4cde96f227&threadid=104060&perpage=15&pagenumber=1

================================================================================

判断当前时间是上午下午还是晚上

select case

when to_number(to_char(sysdate,’hh24′)) between 6 and 11 then ‘上午’

when to_number(to_char(sysdate,’hh24′)) between 11 and 17 then ‘下午’

when to_number(to_char(sysdate,’hh24′)) between 17 and 21 then ‘晚上’

end

from dual;

================================================================================

oracle 中的一些处理日期

将数字转换为任意时间格式.如秒:需要转换为天/小时

select to_char(floor(trunc(936000/(60*60))/24))||’天’||to_char(mod(trunc(936000/(60*60)),24))||’小时’ from dual

to_date格式

day:

dd number 12

dy abbreviated fri

day spelled out friday

ddspth spelled out, ordinal twelfth

month:

mm number 03

mon abbreviated mar

month spelled out march

year:

yy two digits 98

yyyy four digits 1998

24小时格式下时间范围为: 0:00:00 – 23:59:59….

12小时格式下时间范围为: 1:00:00 – 12:59:59 ….

1.

日期和字符转换函数用法(to_date,to_char)

2.

select to_char( to_date(222,’j’),’jsp’) from dual

显示two hundred twenty-two

3.

求某天是星期几

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’) from dual;

星期一

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

monday

设置日期语言

alter session set nls_date_language=’american’;

也可以这样

to_date (‘2002-08-26’, ‘yyyy-mm-dd’, ‘nls_date_language = american’)

4.

两个日期间的天数

select floor(sysdate – to_date(‘20020405′,’yyyymmdd’)) from dual;

5. 时间为null的用法

select id, active_date from table1

union

select 1, to_date(null) from dual;

注意要用to_date(null)

6.

a_date between to_date(‘20011201′,’yyyymmdd’) and to_date(‘20011231′,’yyyymmdd’)

那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。

所以,当时间需要精确的时候,觉得to_char还是必要的

7. 日期格式冲突问题

输入的格式要看你安装的oracle字符集的类型, 比如: us7ascii, date格式的类型就是: ’01-jan-01′

alter system set nls_date_language = american

alter session set nls_date_language = american

或者在to_date中写

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

注意我这只是举了nls_date_language,当然还有很多,

可查看

select * from nls_session_parameters

select * from v$nls_parameters

8.

select count(*)

from ( select rownum-1 rnum

from all_objects

where rownum <= to_date(‘2002-02-28′,’yyyy-mm-dd’) – to_date(‘2002-

02-01′,’yyyy-mm-dd’)+1

)

where to_char( to_date(‘2002-02-01′,’yyyy-mm-dd’)+rnum-1, ‘d’ )

not

in ( ‘1’, ‘7’ )

查找2002-02-28至2002-02-01间除星期一和七的天数

在前后分别调用dbms_utility.get_time, 让后将结果相减(得到的是1/100秒, 而不是毫秒).

9.

select months_between(to_date(’01-31-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1

select months_between(to_date(’02-01-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1.03225806451613

10. next_day的用法

next_day(date, day)

monday-sunday, for format code day

mon-sun, for format code dy

1-7, for format code d

11

select to_char(sysdate,’hh:mi:ss’) time from all_objects

注意:第一条记录的time 与最后一行是一样的

可以建立一个函数来处理这个问题

create or replace function sys_date return date is

begin

return sysdate;

end;

select to_char(sys_date,’hh:mi:ss’) from all_objects;

12.

获得小时数

select extract(hour from timestamp ‘2001-02-16 2:38:40’) from offer

sql> select sysdate ,to_char(sysdate,’hh’) from dual;

sysdate to_char(sysdate,’hh’)

——————– ———————

2003-10-13 19:35:21 07

sql> select sysdate ,to_char(sysdate,’hh24′) from dual;

sysdate to_char(sysdate,’hh24′)

——————– ———————–

2003-10-13 19:35:21 19

获取年月日与此类似

13.

年月日的处理

select older_date,

newer_date,

years,

months,

abs(

trunc(

newer_date-

add_months( older_date,years*12+months )

)

) days

from ( select

trunc(months_between( newer_date, older_date )/12) years,

mod(trunc(months_between( newer_date, older_date )),

12 ) months,

newer_date,

older_date

from ( select hiredate older_date,

add_months(hiredate,rownum)+rownum newer_date

from emp )

)

14.

处理月份天数不定的办法

select to_char(add_months(last_day(sysdate) +1, -2), ‘yyyymmdd’),last_day(sysdate) from dual

16.

找出今年的天数

select add_months(trunc(sysdate,’year’), 12) – trunc(sysdate,’year’) from dual

闰年的处理方法

to_char( last_day( to_date(’02’ ¦ ¦ :year,’mmyyyy’) ), ‘dd’ )

如果是28就不是闰年

17.

yyyy与rrrr的区别

‘yyyy99 to_c

——- —-

yyyy 99 0099

rrrr 99 1999

yyyy 01 0001

rrrr 01 2001

18.不同时区的处理

select to_char( new_time( sysdate, ‘gmt’,’est’), ‘dd/mm/yyyy hh:mi:ss’) ,sysdate

from dual;

19.

5秒钟一个间隔

select to_date(floor(to_char(sysdate,’sssss’)/300) * 300,’sssss’) ,to_char(sysdate,’sssss’)

from dual

2002-11-1 9:55:00 35786

sssss表示5位秒数

20.

一年的第几天

select to_char(sysdate,’ddd’),sysdate from dual

310 2002-11-6 10:03:51

21.计算小时,分,秒,毫秒

select

days,

a,

trunc(a*24) hours,

trunc(a*24*60 – 60*trunc(a*24)) minutes,

trunc(a*24*60*60 – 60*trunc(a*24*60)) seconds,

trunc(a*24*60*60*100 – 100*trunc(a*24*60*60)) mseconds

from

(

select

trunc(sysdate) days,

sysdate – trunc(sysdate) a

from dual

)

select * from tabname

order by decode(mode,’fifo’,1,-1)*to_char(rq,’yyyymmddhh24miss’);

//

floor((date2-date1) /365) 作为年

floor((date2-date1, 365) /30) 作为月

mod(mod(date2-date1, 365), 30)作为日.

23.next_day函数

next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。

1 2 3 4 5 6 7

日 一 二 三 四 五 六

—————————————————————

select (sysdate-to_date(‘2003-12-03 12:55:45′,’yyyy-mm-dd hh24:mi:ss’))*24*60*60 from dual

日期 返回的是天 然后 转换为ss

转此:http://www.onlinedatabase.cn/leadbbs/announce/announce.asp?boardid=42&id=1769

将数字转换为任意时间格式.如秒:需要转换为天/小时

select to_char(floor(trunc(936000/(60*60))/24))||’天’||to_char(mod(trunc(936000/(60*60)),24))||’小时’ from dual

to_date格式

day:

dd number 12

dy abbreviated fri

day spelled out friday

ddspth spelled out, ordinal twelfth

month:

mm number 03

mon abbreviated mar

month spelled out march

year:

yy two digits 98

yyyy four digits 1998

24小时格式下时间范围为: 0:00:00 – 23:59:59….

12小时格式下时间范围为: 1:00:00 – 12:59:59 ….

1.

日期和字符转换函数用法(to_date,to_char)

2.

select to_char( to_date(222,’j’),’jsp’) from dual

显示two hundred twenty-two

3.

求某天是星期几

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’) from dual;

星期一

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

monday

设置日期语言

alter session set nls_date_language=’american’;

也可以这样

to_date (‘2002-08-26’, ‘yyyy-mm-dd’, ‘nls_date_language = american’)

4.

两个日期间的天数

select floor(sysdate – to_date(‘20020405′,’yyyymmdd’)) from dual;

5. 时间为null的用法

select id, active_date from table1

union

select 1, to_date(null) from dual;

注意要用to_date(null)

6.

a_date between to_date(‘20011201′,’yyyymmdd’) and to_date(‘20011231′,’yyyymmdd’)

那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。

所以,当时间需要精确的时候,觉得to_char还是必要的

7. 日期格式冲突问题

输入的格式要看你安装的oracle字符集的类型, 比如: us7ascii, date格式的类型就是: ’01-jan-01′

alter system set nls_date_language = american

alter session set nls_date_language = american

或者在to_date中写

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

注意我这只是举了nls_date_language,当然还有很多,

可查看

select * from nls_session_parameters

select * from v$nls_parameters

8.

select count(*)

from ( select rownum-1 rnum

from all_objects

where rownum <= to_date(‘2002-02-28′,’yyyy-mm-dd’) – to_date(‘2002-

02-01′,’yyyy-mm-dd’)+1

)

where to_char( to_date(‘2002-02-01′,’yyyy-mm-dd’)+rnum-1, ‘d’ )

not

in ( ‘1’, ‘7’ )

查找2002-02-28至2002-02-01间除星期一和七的天数

在前后分别调用dbms_utility.get_time, 让后将结果相减(得到的是1/100秒, 而不是毫秒).

9.

select months_between(to_date(’01-31-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1

select months_between(to_date(’02-01-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1.03225806451613

10. next_day的用法

next_day(date, day)

monday-sunday, for format code day

mon-sun, for format code dy

1-7, for format code d

11

select to_char(sysdate,’hh:mi:ss’) time from all_objects

注意:第一条记录的time 与最后一行是一样的

可以建立一个函数来处理这个问题

create or replace function sys_date return date is

begin

return sysdate;

end;

select to_char(sys_date,’hh:mi:ss’) from all_objects;

12.

获得小时数

select extract(hour from timestamp ‘2001-02-16 2:38:40’) from offer

sql> select sysdate ,to_char(sysdate,’hh’) from dual;

sysdate to_char(sysdate,’hh’)

——————– ———————

2003-10-13 19:35:21 07

sql> select sysdate ,to_char(sysdate,’hh24′) from dual;

sysdate to_char(sysdate,’hh24′)

——————– ———————–

2003-10-13 19:35:21 19

获取年月日与此类似

13.

年月日的处理

select older_date,

newer_date,

years,

months,

abs(

trunc(

newer_date-

add_months( older_date,years*12+months )

)

) days

from ( select

trunc(months_between( newer_date, older_date )/12) years,

mod(trunc(months_between( newer_date, older_date )),

12 ) months,

newer_date,

older_date

from ( select hiredate older_date,

add_months(hiredate,rownum)+rownum newer_date

from emp )

)

14.

处理月份天数不定的办法

select to_char(add_months(last_day(sysdate) +1, -2), ‘yyyymmdd’),last_day(sysdate) from dual

16.

找出今年的天数

select add_months(trunc(sysdate,’year’), 12) – trunc(sysdate,’year’) from dual

闰年的处理方法

to_char( last_day( to_date(’02’ ¦ ¦ :year,’mmyyyy’) ), ‘dd’ )

如果是28就不是闰年

17.

yyyy与rrrr的区别

‘yyyy99 to_c

——- —-

yyyy 99 0099

rrrr 99 1999

yyyy 01 0001

rrrr 01 2001

18.不同时区的处理

select to_char( new_time( sysdate, ‘gmt’,’est’), ‘dd/mm/yyyy hh:mi:ss’) ,sysdate

from dual;

19.

5秒钟一个间隔

select to_date(floor(to_char(sysdate,’sssss’)/300) * 300,’sssss’) ,to_char(sysdate,’sssss’)

from dual

2002-11-1 9:55:00 35786

sssss表示5位秒数

20.

一年的第几天

select to_char(sysdate,’ddd’),sysdate from dual

310 2002-11-6 10:03:51

21.计算小时,分,秒,毫秒

select

days,

a,

trunc(a*24) hours,

trunc(a*24*60 – 60*trunc(a*24)) minutes,

trunc(a*24*60*60 – 60*trunc(a*24*60)) seconds,

trunc(a*24*60*60*100 – 100*trunc(a*24*60*60)) mseconds

from

(

select

trunc(sysdate) days,

sysdate – trunc(sysdate) a

from dual

)

select * from tabname

order by decode(mode,’fifo’,1,-1)*to_char(rq,’yyyymmddhh24miss’);

//

floor((date2-date1) /365) 作为年

floor((date2-date1, 365) /30) 作为月

mod(mod(date2-date1, 365), 30)作为日.

23.next_day函数

next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。

1 2 3 4 5 6 7

日 一 二 三 四 五 六

—————————————————————

select (sysdate-to_date(‘2003-12-03 12:55:45′,’yyyy-mm-dd hh24:mi:ss’))*24*60*60 from dual

日期 返回的是天 然后 转换为ss

转此:http://www.onlinedatabase.cn/leadbbs/announce/announce.asp?boardid=42&id=1769

将数字转换为任意时间格式.如秒:需要转换为天/小时

select to_char(floor(trunc(936000/(60*60))/24))||’天’||to_char(mod(trunc(936000/(60*60)),24))||’小时’ from dual

to_date格式

day:

dd number 12

dy abbreviated fri

day spelled out friday

ddspth spelled out, ordinal twelfth

month:

mm number 03

mon abbreviated mar

month spelled out march

year:

yy two digits 98

yyyy four digits 1998

24小时格式下时间范围为: 0:00:00 – 23:59:59….

12小时格式下时间范围为: 1:00:00 – 12:59:59 ….

1.

日期和字符转换函数用法(to_date,to_char)

2.

select to_char( to_date(222,’j’),’jsp’) from dual

显示two hundred twenty-two

3.

求某天是星期几

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’) from dual;

星期一

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

monday

设置日期语言

alter session set nls_date_language=’american’;

也可以这样

to_date (‘2002-08-26’, ‘yyyy-mm-dd’, ‘nls_date_language = american’)

4.

两个日期间的天数

select floor(sysdate – to_date(‘20020405′,’yyyymmdd’)) from dual;

5. 时间为null的用法

select id, active_date from table1

union

select 1, to_date(null) from dual;

注意要用to_date(null)

6.

a_date between to_date(‘20011201′,’yyyymmdd’) and to_date(‘20011231′,’yyyymmdd’)

那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。

所以,当时间需要精确的时候,觉得to_char还是必要的

7. 日期格式冲突问题

输入的格式要看你安装的oracle字符集的类型, 比如: us7ascii, date格式的类型就是: ’01-jan-01′

alter system set nls_date_language = american

alter session set nls_date_language = american

或者在to_date中写

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

注意我这只是举了nls_date_language,当然还有很多,

可查看

select * from nls_session_parameters

select * from v$nls_parameters

8.

select count(*)

from ( select rownum-1 rnum

from all_objects

where rownum <= to_date(‘2002-02-28′,’yyyy-mm-dd’) – to_date(‘2002-

02-01′,’yyyy-mm-dd’)+1

)

where to_char( to_date(‘2002-02-01′,’yyyy-mm-dd’)+rnum-1, ‘d’ )

not

in ( ‘1’, ‘7’ )

查找2002-02-28至2002-02-01间除星期一和七的天数

在前后分别调用dbms_utility.get_time, 让后将结果相减(得到的是1/100秒, 而不是毫秒).

9.

select months_between(to_date(’01-31-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1

select months_between(to_date(’02-01-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1.03225806451613

10. next_day的用法

next_day(date, day)

monday-sunday, for format code day

mon-sun, for format code dy

1-7, for format code d

11

select to_char(sysdate,’hh:mi:ss’) time from all_objects

注意:第一条记录的time 与最后一行是一样的

可以建立一个函数来处理这个问题

create or replace function sys_date return date is

begin

return sysdate;

end;

select to_char(sys_date,’hh:mi:ss’) from all_objects;

12.

获得小时数

select extract(hour from timestamp ‘2001-02-16 2:38:40’) from offer

sql> select sysdate ,to_char(sysdate,’hh’) from dual;

sysdate to_char(sysdate,’hh’)

——————– ———————

2003-10-13 19:35:21 07

sql> select sysdate ,to_char(sysdate,’hh24′) from dual;

sysdate to_char(sysdate,’hh24′)

——————– ———————–

2003-10-13 19:35:21 19

获取年月日与此类似

13.

年月日的处理

select older_date,

newer_date,

years,

months,

abs(

trunc(

newer_date-

add_months( older_date,years*12+months )

)

) days

from ( select

trunc(months_between( newer_date, older_date )/12) years,

mod(trunc(months_between( newer_date, older_date )),

12 ) months,

newer_date,

older_date

from ( select hiredate older_date,

add_months(hiredate,rownum)+rownum newer_date

from emp )

)

14.

处理月份天数不定的办法

select to_char(add_months(last_day(sysdate) +1, -2), ‘yyyymmdd’),last_day(sysdate) from dual

16.

找出今年的天数

select add_months(trunc(sysdate,’year’), 12) – trunc(sysdate,’year’) from dual

闰年的处理方法

to_char( last_day( to_date(’02’ ¦ ¦ :year,’mmyyyy’) ), ‘dd’ )

如果是28就不是闰年

17.

yyyy与rrrr的区别

‘yyyy99 to_c

——- —-

yyyy 99 0099

rrrr 99 1999

yyyy 01 0001

rrrr 01 2001

18.不同时区的处理

select to_char( new_time( sysdate, ‘gmt’,’est’), ‘dd/mm/yyyy hh:mi:ss’) ,sysdate

from dual;

19.

5秒钟一个间隔

select to_date(floor(to_char(sysdate,’sssss’)/300) * 300,’sssss’) ,to_char(sysdate,’sssss’)

from dual

2002-11-1 9:55:00 35786

sssss表示5位秒数

20.

一年的第几天

select to_char(sysdate,’ddd’),sysdate from dual

310 2002-11-6 10:03:51

21.计算小时,分,秒,毫秒

select

days,

a,

trunc(a*24) hours,

trunc(a*24*60 – 60*trunc(a*24)) minutes,

trunc(a*24*60*60 – 60*trunc(a*24*60)) seconds,

trunc(a*24*60*60*100 – 100*trunc(a*24*60*60)) mseconds

from

(

select

trunc(sysdate) days,

sysdate – trunc(sysdate) a

from dual

)

select * from tabname

order by decode(mode,’fifo’,1,-1)*to_char(rq,’yyyymmddhh24miss’);

//

floor((date2-date1) /365) 作为年

floor((date2-date1, 365) /30) 作为月

mod(mod(date2-date1, 365), 30)作为日.

23.next_day函数

next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。

1 2 3 4 5 6 7

日 一 二 三 四 五 六

—————————————————————

select (sysdate-to_date(‘2003-12-03 12:55:45′,’yyyy-mm-dd hh24:mi:ss’))*24*60*60 from dual

日期 返回的是天 然后 转换为ss

转此:http://www.onlinedatabase.cn/leadbbs/announce/announce.asp?boardid=42&id=1769

将数字转换为任意时间格式.如秒:需要转换为天/小时

select to_char(floor(trunc(936000/(60*60))/24))||’天’||to_char(mod(trunc(936000/(60*60)),24))||’小时’ from dual

to_date格式

day:

dd number 12

dy abbreviated fri

day spelled out friday

ddspth spelled out, ordinal twelfth

month:

mm number 03

mon abbreviated mar

month spelled out march

year:

yy two digits 98

yyyy four digits 1998

24小时格式下时间范围为: 0:00:00 – 23:59:59….

12小时格式下时间范围为: 1:00:00 – 12:59:59 ….

1.

日期和字符转换函数用法(to_date,to_char)

2.

select to_char( to_date(222,’j’),’jsp’) from dual

显示two hundred twenty-two

3.

求某天是星期几

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’) from dual;

星期一

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

monday

设置日期语言

alter session set nls_date_language=’american’;

也可以这样

to_date (‘2002-08-26’, ‘yyyy-mm-dd’, ‘nls_date_language = american’)

4.

两个日期间的天数

select floor(sysdate – to_date(‘20020405′,’yyyymmdd’)) from dual;

5. 时间为null的用法

select id, active_date from table1

union

select 1, to_date(null) from dual;

注意要用to_date(null)

6.

a_date between to_date(‘20011201′,’yyyymmdd’) and to_date(‘20011231′,’yyyymmdd’)

那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。

所以,当时间需要精确的时候,觉得to_char还是必要的

7. 日期格式冲突问题

输入的格式要看你安装的oracle字符集的类型, 比如: us7ascii, date格式的类型就是: ’01-jan-01′

alter system set nls_date_language = american

alter session set nls_date_language = american

或者在to_date中写

select to_char(to_date(‘2002-08-26′,’yyyy-mm-dd’),’day’,’nls_date_language = american’) from dual;

注意我这只是举了nls_date_language,当然还有很多,

可查看

select * from nls_session_parameters

select * from v$nls_parameters

8.

select count(*)

from ( select rownum-1 rnum

from all_objects

where rownum <= to_date(‘2002-02-28′,’yyyy-mm-dd’) – to_date(‘2002-

02-01′,’yyyy-mm-dd’)+1

)

where to_char( to_date(‘2002-02-01′,’yyyy-mm-dd’)+rnum-1, ‘d’ )

not

in ( ‘1’, ‘7’ )

查找2002-02-28至2002-02-01间除星期一和七的天数

在前后分别调用dbms_utility.get_time, 让后将结果相减(得到的是1/100秒, 而不是毫秒).

9.

select months_between(to_date(’01-31-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1

select months_between(to_date(’02-01-1999′,’mm-dd-yyyy’),

to_date(’12-31-1998′,’mm-dd-yyyy’)) “months” from dual;

1.03225806451613

10. next_day的用法

next_day(date, day)

monday-sunday, for format code day

mon-sun, for format code dy

1-7, for format code d

11

select to_char(sysdate,’hh:mi:ss’) time from all_objects

注意:第一条记录的time 与最后一行是一样的

可以建立一个函数来处理这个问题

create or replace function sys_date return date is

begin

return sysdate;

end;

select to_char(sys_date,’hh:mi:ss’) from all_objects;

12.

获得小时数

select extract(hour from timestamp ‘2001-02-16 2:38:40’) from offer

sql> select sysdate ,to_char(sysdate,’hh’) from dual;

sysdate to_char(sysdate,’hh’)

——————– ———————

2003-10-13 19:35:21 07

sql> select sysdate ,to_char(sysdate,’hh24′) from dual;

sysdate to_char(sysdate,’hh24′)

——————– ———————–

2003-10-13 19:35:21 19

获取年月日与此类似

13.

年月日的处理

select older_date,

newer_date,

years,

months,

abs(

trunc(

newer_date-

add_months( older_date,years*12+months )

)

) days

from ( select

trunc(months_between( newer_date, older_date )/12) years,

mod(trunc(months_between( newer_date, older_date )),

12 ) months,

newer_date,

older_date

from ( select hiredate older_date,

add_months(hiredate,rownum)+rownum newer_date

from emp )

)

14.

处理月份天数不定的办法

select to_char(add_months(last_day(sysdate) +1, -2), ‘yyyymmdd’),last_day(sysdate) from dual

16.

找出今年的天数

select add_months(trunc(sysdate,’year’), 12) – trunc(sysdate,’year’) from dual

闰年的处理方法

to_char( last_day( to_date(’02’ ¦ ¦ :year,’mmyyyy’) ), ‘dd’ )

如果是28就不是闰年

17.

yyyy与rrrr的区别

‘yyyy99 to_c

——- —-

yyyy 99 0099

rrrr 99 1999

yyyy 01 0001

rrrr 01 2001

18.不同时区的处理

select to_char( new_time( sysdate, ‘gmt’,’est’), ‘dd/mm/yyyy hh:mi:ss’) ,sysdate

from dual;

19.

5秒钟一个间隔

select to_date(floor(to_char(sysdate,’sssss’)/300) * 300,’sssss’) ,to_char(sysdate,’sssss’)

from dual

2002-11-1 9:55:00 35786

sssss表示5位秒数

20.

一年的第几天

select to_char(sysdate,’ddd’),sysdate from dual

310 2002-11-6 10:03:51

21.计算小时,分,秒,毫秒

select

days,

a,

trunc(a*24) hours,

trunc(a*24*60 – 60*trunc(a*24)) minutes,

trunc(a*24*60*60 – 60*trunc(a*24*60)) seconds,

trunc(a*24*60*60*100 – 100*trunc(a*24*60*60)) mseconds

from

(

select

trunc(sysdate) days,

sysdate – trunc(sysdate) a

from dual

)

select * from tabname

order by decode(mode,’fifo’,1,-1)*to_char(rq,’yyyymmddhh24miss’);

//

floor((date2-date1) /365) 作为年

floor((date2-date1, 365) /30) 作为月

mod(mod(date2-date1, 365), 30)作为日.

23.next_day函数

next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。

1 2 3 4 5 6 7

日 一 二 三 四 五 六

—————————————————————

select (sysdate-to_date(‘2003-12-03 12:55:45′,’yyyy-mm-dd hh24:mi:ss’))*24*60*60 from dual

日期 返回的是天 然后 转换为ss


1

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐