Oracle trunc 函式用法詳解

2021-08-17 10:32:57 字數 1242 閱讀 1980

用於擷取時間或者數值,返回指定的值
一、擷取時間

select  trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'yyyy') from   dual ;

--返回當年第一天

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'mm') from dual ;

--返回當月第一天

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'dd') from dual ;

--返回當前年月

select trunc(to_date('2018-02-01 1:00:00','yyyy-mm-dd hh:mi:ss'),'d') from dual ;

--返回當前星期的第一天(星期日)

select trunc(to_date('2018-02-01 1:12:12','yyyy-mm-dd hh:mi:ss'),'hh') from dual ;

--返回當前日期擷取到小時,分秒補0

select trunc(to_date('2018-02-01 1:12:12','yyyy-mm-dd hh:mi:ss'),'mi') from dual ;

--返回當前日期擷取到分,秒補0

執行結果如下圖

一、擷取數值

語法:trunc(number,decimals)

number:指需要擷取的數字,

select  trunc(122.555) from  dual t;

--預設取整

select trunc(122.555,2) from dual t;

select trunc(122.555,-2) from dual t;

--負數表示從小數點左邊開始擷取2位

執行結果

ORACLE TRUNC函式用法

1.trunc for dates trunc函式為指定元素而截去的日期值。其具體的語法格式如下 trunc date fmt 其中 date 乙個日期值 fmt 日期格式,該日期將由指定的元素格式所截去。忽略它則由最近的日期截去 下面是該函式的使用情況 trunc to date 24 nov 1...

Oracle trunc函式用法

日期 1.select trunc sysdate from dual 2013 01 06 今天的日期為2013 01 06 2.select trunc sysdate,mm from dual 2013 01 01 返回當月第一天.3.select trunc sysdate,yy from ...

Oracle trunc 函式用法

oracle trunc 函式的用法 日期 1.select trunc sysdate from dual 2013 01 06 今天的日期為2013 01 06 2.select trunc sysdate,mm from dual 2013 01 01 返回當月第一天.3.select tru...