oracle-關於時間的sql

如下內容來源:http://www.cnblogs.com/hl3292/archive/2010/11/03/1868159.html html

 

轉換的格式:session

 表示 year 的:y 表示年的最後一位 、spa

                     yy 表示年的最後2位 、 
                     yyy 表示年的最後3位 、
                     yyyy 用4位數表示年htm

表示month的: mm 用2位數字表示月 、
                       mon 用簡寫形式, 好比11月或者nov 、
                       month 用全稱, 好比11月或者novemberblog

表示day的:dd  表示當月第幾天 、
                  ddd 表示當年第幾天 、
                  dy  當週第幾天,簡寫, 好比星期五或者fri 、
                  day 當週第幾天,全稱, 好比星期五或者friday字符串

表示hour的:hh   2位數表示小時 12進制、 
                   hh24 2位數表示小時 24小時it

表示minute的:mi 2位數表示分鐘io

表示second的:ss 2位數表示秒 60進制ast

表示季度的:q 一位數 表示季度 (1-4)date

另外還有ww 用來表示當年第幾周 w用來表示當月第幾周。

24小時制下的時間範圍:00:00:00-23:59:59

12小時制下的時間範圍:1:00:00-12:59:59

 

當前時間 sysdate 2012/07/31

 

把日期或數字轉換爲字符串to_char(sysdate(),'yy-mm-dd hh24:mi:ss')

to_char(number, '格式') , 例子:to_char('1000', '$99,999.99')   //輸出$1,000.00
to_char(date, '格式')

字符串轉換爲時間to_date('2009-12-25 14:23:31','yyyy-mm-dd,hh24:mi:ss')

將字符串轉換爲數字select to_number('1000000.123') from dual; //輸出 1000000.123'

當前時間減去時分秒select sysdate - 8*interval '7' hour from dual; //當前時間減去8個7小時。hour可換成second,day,minute,month,year

當前日期d後推n個月select add_months(sysdate,2) from dual; //n可爲負數,輸出: 30-9月 -13

本月最後一天:select last_day(sysdate) from dual; //輸出 31-7月 -13

日期f和s間相差月數select months_between(sysdate,to_date('2005-11-12','yyyy-mm-dd'))from dual; //輸出 92.63361185782556750298685782556750298686

當前會話時區中的當前日期:select current_date from dual; //輸出 31-7月 -13

以timestamp with time zone數據類型返回當前會話時區中的當前日期:select sessiontimezone,current_timestamp from dual;

//輸出 31-7月 -13 03.27.43.445033000 下午 +08:00

返回時區select dbtimezone from dual

截取時間:select trunc(sysdate ,'yyyy') from dual;  輸出:01-1月 -13 (截取到年)

 

按照每週統計select to_char(sysdate,'ww') from dual group by to_char(sysdate,'ww'); //同理,可按每個月,年等統計

相關文章
相關標籤/搜索