to_number(to_char(sysdate,'yyyy')) - to_number(to_char(day1,'yyyy')) as yeardel ,
to_number(to_char(sysdate, 'mm')) - to_number(to_char(day1, 'mm')) as mothdel,
to_number(to_char(sysdate, 'dd')) - to_number(to_char(day1, 'dd')) as daydel 这样是年月日,分开来相减的,这样减出来后,月和日 可能有负数,所以还得判断。假如day1的值为2008-12-3 则结果是 年3,月-1,日4 而不是 年2,月11,日4
所以想用其他的办法.
trunc((months_between(trunc(sysdate,'dd'), day1))/12) as year 这样,结果是单纯的年,并且是正确的年.我想用这种类似的函数计算出月和日. 可是不知道怎么使用函数??
to_number(to_char(sysdate, 'mm')) - to_number(to_char(day1, 'mm')) as mothdel,
to_number(to_char(sysdate, 'dd')) - to_number(to_char(day1, 'dd')) as daydel 这样是年月日,分开来相减的,这样减出来后,月和日 可能有负数,所以还得判断。假如day1的值为2008-12-3 则结果是 年3,月-1,日4 而不是 年2,月11,日4
所以想用其他的办法.
trunc((months_between(trunc(sysdate,'dd'), day1))/12) as year 这样,结果是单纯的年,并且是正确的年.我想用这种类似的函数计算出月和日. 可是不知道怎么使用函数??
解决方案 »
- IE6,IE7,IE8在页面显示上有什么区别?
- 请教:将SQL Server中的资料导出到GridView时行号的问题,详情见内
- 双击textbox控件,弹出一个新窗口,如何实现。为什么我写的函数是服务器端函数,如何转换为客户端函数的。
- 关于 asp.net 中,用 VB.net 打开 access 数据库的问题?
- 如何在一html文件中插入一张图片,并且能文字环绕
- .aspx文件中定义了<li>标签的onclick方法,为何运行时单击无效
- 急救 ...水晶报表
- 怎样根据数据库里某个字段的值确定ItemTemplate里的CheckBox控件是否该选中啊
- DataSet怎么样来计算数据库里有多少 ?
- 请问各位大虾,入门用哪本书比较合适?
- html 转化成word图片不显示怎么解决啊
- lock不生效
select datediff(month,'2004-01-01',getdate()) 相差月数
select datediff(year,'2004-01-01',getdate()) 相差年数
select datediff(week,'2004-01-01',getdate()) 相差星期
select datediff(Quarter,'2004-01-01',getdate()) 相差季度
select ABS(to_number(to_char(to_date('2009-12-12','yyyy-mm-dd'),'yyyy')) - to_number(to_char(to_date('2011-12-12','yyyy-mm-dd'), 'yyyy'))) from dual;月数:
with temp as(
select to_date('2006-05-01','yyyy-mm-dd') time2,to_date('2009-06-08','yyyy-mm-dd') time1 from dual
union all
select to_date('2008-12-01','yyyy-mm-dd') time2,to_date('2010-02-10','yyyy-mm-dd') time1 from dual
)
select case when to_number(to_char(time1,'MM')) - to_number(to_char(time2,'MM')) >= 0
then to_number(to_char(time1,'MM')) - to_number(to_char(time2,'MM'))
else to_number(to_char(time1,'MM'))+12 - to_number(to_char(time2,'MM')) + 1
end 相差月数
from temp
天数差:
select floor(to_date( '20110409 ', 'yyyymmdd ') - to_date( '20110408 ', 'yyyymmdd ')) from dual;