select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //日期转化为字符串
select to_char(sysdate,'yyyy') as nowYear from dual; //获取时间的年
select to_char(sysdate,'mm') as nowMonth from dual; //获取时间的月
select to_char(sysdate,'dd') as nowDay from dual; //获取时间的日
select to_char(sysdate,'hh24') as nowHour from dual; //获取时间的时
select to_char(sysdate,'mi') as nowMinute from dual; //获取时间的分
select to_char(sysdate,'ss') as nowSecond from dual; //获取时间的秒
2. 字符串和时间互转
[url=]复制代码[/url] 代码如下:
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual
select to_char( to_date(222,'J'),'Jsp') from dual //显示Two Hundred Twenty-Two
3.求某天是星期几
[url=]复制代码[/url] 代码如下:
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. 两个日期间的天数
[url=]复制代码[/url] 代码如下:
select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;
5. 时间为null的用法
[url=]复制代码[/url] 代码如下:
select id, active_date from table1
UNION
select 1, TO_DATE(null) from dual; //注意要用TO_DATE(null)
6.月份差
[url=]复制代码[/url] 代码如下:
a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')
//那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。
//所以,当时间需要精确的时候,觉得to_char还是必要的
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.查询特殊条件天数
[url=]复制代码[/url] 代码如下:
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' )
select sysdate S1,
round(sysdate) S2 ,
round(sysdate,'year') YEAR,
round(sysdate,'month') MONTH ,
round(sysdate,'day') DAY from dual
22,trunc[截断到最接近的日期,单位为天] ,返回的是日期类型
[url=]复制代码[/url] 代码如下:
select sysdate S1,
trunc(sysdate) S2, //返回当前日期,无时分秒
trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒
trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒
trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒
from dual
23,返回日期列表中最晚日期
[url=]复制代码[/url] 代码如下:
select greatest('01-1月-04','04-1月-04','10-2月-04') from dual
24.计算时间差
[url=]复制代码[/url] 代码如下:
注racle时间差是以天数为单位,所以换算成年月,日
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
'yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual //时间差-年
select ceil(moths_between(sysdate-to_date('2007-11-02 15:55:03',
'yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual //时间差-月
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
'yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual //时间差-天
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
'yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual //时间差-时
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
'yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual //时间差-分
select floor(to_number(sysdate-to_date('2007-11-02 15:55:03',
'yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //时间差-秒
25.更新时间
[url=]复制代码[/url] 代码如下:
//oracle时间加减是以天数为单位,设改变量为n,所以换算成年月,日
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n*365,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-年
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
add_months(sysdate,n) as newTime from dual //改变时间-月
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-日
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n/24,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-时
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n/24/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-分
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),
to_char(sysdate+n/24/60/60,'yyyy-mm-dd hh24:mi:ss') as newTime from dual //改变时间-秒