时间差计算:
两个Date类型字段:START_DATE,END_DATE,计算这两个日期的时间差(分别以天,小时,分钟,秒,毫秒):
天:
ROUND(TO_NUMBER(END_DATE - START_DATE))
小时:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24)
分钟:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60)
秒:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60 * 60)
毫秒:
ROUND(TO_NUMBER(END_DATE - START_DATE) * 24 * 60 * 60 * 1000)
要将时间格式为 yyyy/mm/dd hh24:mi:ss的时间变成yyyy/mm/dd
可用trunc()函数:trunc(yyyy/mm/dd hh24:mi:ss)------>yyyy/mm/dd
trunc还可用于截取数字
注意Oracle与MySQL不同,不能用to_char(time,‘yyyy/mm/dd hh:mm:ss‘),应该用to_char(time,‘yyyy/mm/dd hh24:mi:ss‘)
日期和字符转换函数用法(to_date,to_char)
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; //获取时间的秒