oracle的trunc()函数是一个截取函数,也就是说,这个函数也可以用来截取字段中的字符,这里我来说截取时间。
trunc()的基本格式:trunc(datetime,String str)
datetime代表时间,字段也好,时间也行,str代表要截取的时间。
上例子:
//取年份,表示该年1月1号,00:00::00
select trunc(sysdate,'yy') from dual;
//取月份,表示该年该月1号,00:00:00
select trunc(sysdate,'mm') from dual;
//去星期,表示该周的周一,00:00:00
select trunc(sysdate,'d') from dual;
如果是只是一个trunc(sysdate)是正常显示的。当天的00:00:00。
然后是对时间的截取:
//当前天,当前点,00分00秒
select trunc(sysdate,'hh') from dual;
//当前天,当前点,当前分00秒
select trunc(sysdate,'mi') from dual;
为啥是mi,因为mm代表月份,如果分钟再用mm,就会报错,这就是为啥:
to_date(,'yyyy-MM-dd hh24:mi:ss')
to_char(,'yyyy-MM-dd hh2:mi:ss')