2014-10-30 64 views
0

我想和顯示日期,小時和分鐘分開(3列:日期,小時和分鐘)如何在oracle sql中獲取小時和分鐘?

我的腳本不起作用。如何解決這個問題?謝謝

這裏的SQL:

select trunc(t.create_time, 'DD') as createdate 
      ,trunc(t.close_time, 'DD') as closedate 
      ,datepart(hour, t.close_time()) as hours 
      ,datepart(minute, t.close_time()) as minutes 
      ,count(t.close_time) as total 
     from app_account.otrs_ticket t 
     left join app_account.otrs_user u 
     on t.create_user_id=u.id 
     where u.id not in (15,7,31,49,50,52,62,66,69,106,17,24,44,32,33,55,22,29,30,47,45,53,70,74,109,1,2,10,23,68,80,20,21,56,108,67) 
     group by trunc(t.create_time, 'DD') 
       ,trunc(t.close_time, 'DD') 
       ,datepart(hour, t.close_time()) 
       ,datepart(minute, t.close_time()) 
     order by trunc(t.create_time, 'DD') desc 

回答

3

我發現to_char()函數是轉換和提取日期部分最靈活的函數。

這裏是提取自SYSDATE的各種元素的例子:

select to_char(sysdate, 'YYYY') as year, 
     to_char(sysdate, 'MM') as month, 
     to_char(sysdate, 'DD') as day, 
     to_char(sysdate, 'HH24') as hour, 
     to_char(sysdate, 'MI') as minute 
from dual 

您可以提供不同格式的參數,以獲得任何結果,您需要。

0

Oracle的功能extract()to_char()

select trunc(t.create_time, 'DD') as createdate, 
     trunc(t.close_time, 'DD') as closedate, 
     extract(hour from t.close_time) as hours, 
     extract(minute from t.close_time) as minutes, 
     count(t.close_time) as total 
from app_account.otrs_ticket t left join 
     app_account.otrs_user u 
     on t.create_user_id=u.id 
where u.id not in (15,7,31,49,50,52,62,66,69,106,17,24,44,32,33,55,22,29,30,47,45,53,70,74,109,1,2,10,23,68,80,20,21,56,108,67) 
group by trunc(t.create_time, 'DD'), trunc(t.close_time, 'DD') 
      extract(hour from t.close_time) as hours, 
      extract(minute from t.close_time) as minutes 
order by trunc(t.create_time, 'DD') desc; 

我不知道什麼是()close_time,但他們似乎並沒有必要。

+0

我測試你的腳本,我得到一個錯誤:ORA-30076:提取源的無效提取字段 30076. 00000 - 「提取源的無效提取字段」 *原因:提取源不包含指定的提取字段。 – User014019 2014-10-30 03:25:07

+0

這些功能在Oracle中可用一段時間:http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm。 – 2014-10-30 03:30:36

+0

't.close_time'可能不是日期/時間值嗎? – 2014-10-30 03:31:44