用sql语言怎么计算 如:1981年2月3日到1981年11月25日的时间 求代码。

如题所述

第1个回答  2011-01-11
select round(((19810203-19811125)/365),0) from dual;

结果:-3
两个数相减然后取整,这是年数,他们相差3年,不取整去掉round函数 应该是2.5年;本回答被网友采纳
第2个回答  2011-01-11
SqlServer中:
datediff()函数返回两个日期之间的天数。
datediff(datepart,startDate,endDate)
********************************************
Oracle中:
以“天”计算:round(to_bumber(endDate-startDate))
以“小时”计算:round(to_bumber(endDate-startDate)*24)
以“分钟”计算:round(to_bumber(endDate-startDate)*24*60)
以“秒”计算:round(to_bumber(endDate-startDate)*24*60*60)
以“毫秒”计算:round(to_bumber(endDate-startDate)*24*60*60*60)
可以参考:http://www.javaeye.com/problems/9918
第3个回答  2011-01-12
sql有个datediff()函数计算2个时间差的
例如:
2个时间段相差天数
select DATEDIFF (day,'1981-2-3','1981-11-25' )
2个时间段相差月数
select DATEDIFF (month,'1981-2-3','1981-11-25' )
第4个回答  2011-01-11
这样 比如说 你的日期字段 是 date 就是 select * from xx表 where date>='1981-2-3' and date<='1981-11-25'
第5个回答  2011-01-11
额 得出的单位什么?是天数还是?
其实可以直接减 select (1981-02-03)-(1981-11-25) as Day
相似回答
大家正在搜