彩虹天堂
技术源于生活
posts - 0,  comments - 2,  trackbacks - 0

--5.17 时间和日期处理函数

select * from products;
--检索年份是2007的所有 prod_end数据
select * from products
where to_number(to_char(prod_end,'YYYY'))= 2007;

select * from products
where prod_end between to_date('20060708','yyyy-mm-dd')
and to_date('20080708','yyyy-mm-dd');

--求某天是星期几
select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;

--两个日期间的天数
select floor(sysdate - to_date('20080516','yyyymmdd')) from dual;
select ceil(to_date('20080518101812','yyyymmdd hh:mi:ss') - to_date('20080516101815','yyyymmdd hh:mi:ss')) from dual;--2  天花板函数,即加入正小数至最近整数
select floor(to_date('20080518101812','yyyymmdd hh:mi:ss') - to_date('20080516101815','yyyymmdd hh:mi:ss')) from dual;--1 地板函数,即舍去正小数至最近整数

--查找2008-05-03至2008-05-12间除星期一和七的天数
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2008-05-12','yyyy-mm-dd') - to_date('2008-05-03','yyyy-mm-dd')+1)
where to_char( to_date('2008-05-03','yyyy-mm-dd')+rnum-1, 'D' )
not in ( '1', '7' )

--获取05-03-2008至06-02-2008间有多少个月,如果一个月以下的,返回小数,如这个例子返回:0.967741935483871
select months_between(to_date('06-02-2008','MM-DD-YYYY'),
to_date('05-03-2008','MM-DD-YYYY')) "MONTHS" FROM DUAL;

--获取小时,分钟,秒,年月日
select sysdate ,to_char(sysdate,'hh') from dual;
select to_char('sdsf','sd') from dual;

--找出今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual

--闰年的处理方法
select to_char( last_day( to_date('02' || '2008','mmyyyy') ), 'dd' ) from dual

--一年的第几天
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual

--是从当前开始下一个星期五的具体时间
select next_day(sysdate,6) from dual


--floor((date2-date1) /365) 作为年
--floor((date2-date1, 365) /30) 作为月
--mod(mod(date2-date1, 365), 30)作为日.


--to_char,函数功能,就是将数值型或者日期型转化为字符型,FM :除空格 
--9999999.0099:允许小数点左边最大正数为7位,小数点右边最少2位,最多4位,且在第5位进行四舍五入
select TO_CHAR(123.02,'FM9999999.00009') from DUAL
select TO_CHAR(0.123,'FM0.999') from DUAL

posted on 2008-05-17 11:13 bcterry 阅读(173) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

留言簿

文章档案

搜索

  •  

最新评论