【数据库】SQL日常函数之日期处理,小数点处理

SQL之日期处理 trunc

select trunc(sysdate) from dual; --2017/11/15 今天的日期为2017-11-15
select trunc(sysdate, 'mm') from dual; --2017/11/1 返回当月第一天.
select trunc(sysdate,'yy') from dual; --2017/1/1 返回当年第一天
select trunc(sysdate,'dd') from dual; --2017/11/15 返回当前年月日
select trunc(sysdate,'yyyy') from dual; --2017/1/1 返回当年第一天
select trunc(sysdate,'d') from dual; --2017/11/12(星期天)返回当前星期的第一天
select trunc(sysdate, 'hh') from dual; --2017/11/15 14:00:00 当前时间为17:35 
select trunc(sysdate, 'mi') from dual; --2017/11/15 14:55:00 TRUNC()函数没有秒的精确
trunc()截取函数:逻辑截取时间

取当前时间之前(-)/之后(+)一段的时间

*单位要注意*
trunc(sysdate+1/24,'HH') 是取当前系统时间,舍去分秒。
+1/24 就是加上一小时。
以此类推
sysdate+1 加一天
sysdate+1/24 加1小时
sysdate+1/(24*60) 加1分钟
sysdate+1/(24*60*60) 加1秒钟

SQL之小数点后位数处理 round

查jx_score表的平均值,以哪次考试(testid)和科目分组(courseid)
select
testid,
courseid,
round(avg(`jx_score`.`score`),2) AS `average`
from `jx_score`
group by `jx_score`.`testid`,`jx_score`.`courseid`
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章