Oracle trunc函数使用

select to_char(sysdate, ‘yyyy-mm-dd hh24:mi:ss‘),
       to_char(trunc(sysdate), ‘yyyy-mm-dd hh24:mi:ss‘)
  from dual t;--sysdate和trunc(sysdate)是不一样的 sysdate在当天的零时零分零秒等于trunc(sysdate)

select trunc(sysdate, ‘dd‘), trunc(sysdate) from dual t; --今天 20140703 零时零分零秒
select to_char(trunc(sysdate, ‘dd‘), ‘yyyy/mm/dd hh24:mi:ss‘),
       to_char(trunc(sysdate), ‘yyyy/mm/dd hh24:mi:ss‘)
  from dual;

select trunc(sysdate-to_date(‘20140709 23:12:12‘,‘yyyy/mm/dd hh24:mi:ss‘)) from dual t;--两个日期相差的天数
select trunc(sysdate-1) from dual t;--昨天
select trunc(sysdate+1) from dual t;--明天

select trunc(sysdate, ‘yy‘) from dual;--当年第一天
select trunc(sysdate, ‘year‘) from dual;--当年第一天
select trunc(sysdate, ‘yyyy‘) from dual;--当年第一天

select trunc(sysdate, ‘q‘) from dual;--当前时间所在的季度的第一天

select trunc(sysdate, ‘mm‘) from dual;--当月第一天
select trunc(sysdate, ‘month‘) from dual;--当月第一天

select trunc(sysdate, ‘d‘) from dual;--返回本周的第一天(周日为第一天)
select trunc(sysdate,‘day‘) from dual;--返回本周的第一天(周日为第一天)

select trunc(sysdate, ‘iw‘) from dual;--本周第二天(周日为第一天)

select trunc(sysdate, ‘hh‘) from dual; --当前时间,精确到小时
select trunc(sysdate, ‘hh24‘) from dual;--当前时间。精确到小时

select trunc(sysdate, ‘mi‘) from dual;--当前时间。精确到分钟 没有精确到秒的精度
 
/*
TRUNC(number,num_digits)
Number 须要截尾取整的数字。
Num_digits 用于指定取整精度的数字。

Num_digits 的默认值为 0。 正数向后截取,负数向前截取 TRUNC()函数截取时不进行四舍五入 */ select trunc(123.458) from dual; --123 select trunc(123.458, 0) from dual; --123 select trunc(123.458, 1) from dual; --123.4 select trunc(123.458, -1) from dual; --120 select trunc(123.458, -4) from dual; --0 select trunc(123.458, 4) from dual; --123.458 select trunc(123) from dual; --123 select trunc(123, 1) from dual; --123 select trunc(123, -1) from dual; --120

原文地址:https://www.cnblogs.com/ConfidentLiu/p/10279050.html

时间: 2024-08-09 13:39:41

Oracle trunc函数使用的相关文章

Oracle trunc()函数的用法

--Oracle trunc()函数的用法 /**************日期  TRUNC()函数没有秒的精确 ********************/ select sysdate from dual --当时日期 select trunc(sysdate) from dual select trunc(sysdate ,'DD') from dual --今天日期 select trunc(sysdate,'d')+7 from dual --本周星期日 select trunc(sys

[转]Oracle trunc()函数的用法

原文地址:http://www.cnblogs.com/gengaixue/archive/2012/11/21/2781037.html 1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC(date[,fmt]) 其中: date 一个日期值 fmt 日期格式,该日期将由指定的元素格式所截去.忽略它则由最近的日期截去 下面是该函数的使用情况: TRUNC(TO_DATE('24-Nov-1999 08:00 pm'),'dd-mo

oracle trunc 函数处理日期格式

oracle trunc 函数处理日期格式 select TRUNC(LAST_DAY(SYSDATE))+29+20/24 from dual--下个月的某一天几点几分 2015/11/29 20:00:00 select add_months(TRUNC(SYSDATE,'mm'),1)+5/24 from dual--下个月1号的几点2015/11/1 5:00:00 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;  -

Oracle trunc()函数

--Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013-01-06 今天的日期为2013-01-062.select trunc(sysdate, 'mm') from dual --2013-01-01 返回当月第一天.3.select trunc(sysdate,'yy') from dual --2013-01-01 返回当年第一天4.select

oracle trunc函数

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss'), to_char(trunc(sysdate), 'yyyy-mm-dd hh24:mi:ss') from dual t;--sysdate和trunc(sysdate)是不一样的 sysdate在当天的零时零分零秒等于trunc(sysdate) select trunc(sysdate, 'dd'), trunc(sysdate) from dual t; --今天 20140703 零时零分

oracle获取本月第一天和最后一天及Oracle trunc()函数的用法

select to_char(trunc(add_months(last_day(sysdate), -1) + 1), 'yyyy-mm-dd') "本月第一天",to_char(last_day(sysdate), 'yyyy-mm-dd') "本月最后一天"–Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual  –2011-3-18

oracle trunc()函数用法-1

1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC(date[,fmt]) 其中: date 一个日期值 fmt 日期格式,该日期将由指定的元素格式所截去.忽略它则由最近的日期截去 下面是该函数的使用情况: TRUNC(TO_DATE('24-Nov-1999 08:00 pm','dd-mon-yyyy hh:mi am')) ='24-Nov-1999 12:00:00 am'

oracle trunc()函数用法-2

2.TRUNC(for number) TRUNC函数返回处理后的数值,其工作机制与ROUND函数极为类似,只是该函数不对指定小数前或后的部分做相应舍入选择处理,而统统截去. 其具体的语法格式如下 TRUNC(number[,decimals]) 其中: number 待做截取处理的数值 decimals 指明需保留小数点后面的位数.可选项,忽略它则截去所有的小数部分 下面是该函数的使用情况: TRUNC(89.985,2)=89.98

oracle trunc 函数处理日期格式,

select to_char(sysdate, 'yyyy-mm-dd hh24:mi:ss') from dual; --显示当前时间 2017 - 04 - 24 18 :42 :17 select trunc(sysdate, 'year') from dual; --截取到年(本年的第一天) 2017 / 1 / 1 select trunc(sysdate, 'q') from dual; --截取到季度(本季度的第一天) 2017 / 4 / 1 select trunc(sysda