---
title: "to_date() 时间相减 截取"
author: "disablebb"
date: "2011-12-16T10:07:08Z"
tags: []
categories: ["数据库"]
slug: "to-date"
draft: false
lastmod: "2011-12-16T10:13:10Z"
id: 1313557
url: "https://disablebb.iteye.com/blog/1313557"
---

to\_date() 时间相减

select to\_date(untreat\_date,'yyyy-mm-dd hh24:mi:ss')-to\_date(reg\_date,'yyyy-mm-dd hh24:mi:ss')  
  from XXX where to\_date(untreat\_date,'yyyy-mm-dd hh24:mi:ss')-to\_date(reg\_date,'yyyy-mm-dd hh24:mi:ss')&gt;0

 

TRUNC（date\[,fmt]） 截取

      date 一个日期值   
　　fmt 日期格式，该日期将由指定的元素格式所截去。忽略它则由最近的日期截去

      trunc(sysdate,'yyyy') --返回当年第一天.   
　　trunc(sysdate,'mm') --返回当月第一天.  
　　trunc(sysdate,'d') --返回当前星期的第一天.  
　　trunc(sysdate,'dd')--返回当前年月日

 

select status,count(\*) countnum,CAST(sum(time) as decimal(38, 0))  
sumtime from XXX where indate&gt;trunc(sysdate-2)  
AND indate&lt;trunc(sysdate) group by status