2014-10-20 62 views
0

我試圖從格式提取小時和分鐘不工作

2012/12/01 02:03:44 

我的代碼的日期爲Extrac小時和分鐘看起來像水木清華

SELECT to_char(t.start_date, 'DD-MON-YY HH24:MI:SS')        AS Start_DATE, 
     to_char(t.resolved_date, 'DD-MON-YY HH24:MI:SS')       AS Closed_DATE, 
     extract(MINUTE FROM TO_TIMESTAMP(t.start_date, 'YYYY/MON/DD HH24:MI:SS')) AS Start_DATE_min, 
     extract(MINUTE FROM TO_TIMESTAMP(t.closed_date, 'YYYY/MON/DD HH24:MI:SS')) AS Closed_DATE_min, 
     extract(HOUR FROM TO_TIMESTAMP(t.start_date, 'DD-MON-YYYY HH24:MI:SS')) AS Start_DATE_h, 
     extract(HOUR FROM TO_TIMESTAMP(t.closed_date, 'DD-MON-YYYY HH24:MI:SS')) AS Closed_DATE_h 
FROM t 

但在提取領域我只是得到0。

任何想法我做錯了什麼?

+0

什麼不起作用? – Kermit 2014-10-20 14:15:51

回答

0

嘗試:

SELECT to_char(t.start_date, 'DD-MON-YY HH24:MI:SS')    AS Start_DATE, 
     to_char(t.resolved_date, 'DD-MON-YY HH24:MI:SS')    AS Closed_DATE, 
     extract(MINUTE FROM t.start_date ) AS Start_DATE_min, 
     extract(MINUTE FROM t.closed_date) AS Closed_DATE_min, 
     extract(HOUR FROM t.start_date ) AS Start_DATE_h, 
     extract(HOUR FROM t.closed_date) AS Closed_DATE_h 
FROM t 

我假設你正在使用的日期時間格式存儲日期和你並不需要將它們轉化爲時間戳。

UPDATE:如果您有DATE類型,則從沒有此信息的類型中提取分鐘和小時信息是沒有意義的。 它解釋了你爲什麼要讀零的原因將數據類型更改爲DATETIME。這就像從INTEGER中提取小數。或者只是把零和節省CPU:

SELECT to_char(t.start_date, 'DD-MON-YY HH24:MI:SS')    AS Start_DATE, 
     to_char(t.resolved_date, 'DD-MON-YY HH24:MI:SS')    AS Closed_DATE, 
     0 AS Start_DATE_min, 
     0 AS Closed_DATE_min, 
     0 AS Start_DATE_h, 
     0 AS Closed_DATE_h 
FROM t 

附:在問題中它說:「我試圖從格式爲 2012/12/01 02:03:44的日期中提取小時和分鐘」。如何在不丟失時間信息的情況下將其存儲在DATE中?

+0

如果start_date和closed_date具有DATE類型,則無法從它們中提取有關ANSI SQL的時間! – Multisync 2014-10-20 15:14:34

+0

那麼這是OP的問題,這就是爲什麼他會收到所有0,如果他們存儲爲'DATE',對吧? – AdamMc331 2014-10-20 15:28:31

+0

@ McAdam331我認爲這是答案的問題,因爲如果start_date和closed_date的日期類型爲 – Multisync 2014-10-20 15:30:41

2
SELECT to_char(t.start_date, 'DD-MON-YY HH24:MI:SS')        AS Start_DATE, 
     to_char(t.resolved_date, 'DD-MON-YY HH24:MI:SS')       as closed_date, 
     extract(minute from cast(t.start_date as timestamp)) as start_date_min, 
     extract(MINUTE FROM cast(t.closed_date as timestamp)) AS Closed_DATE_min, 
     extract(HOUR FROM cast(t.start_date as timestamp)) AS Start_DATE_h, 
     extract(HOUR FROM cast(t.closed_date as timestamp)) AS Closed_DATE_h 
FROM t 



SELECT to_char(t.start_date, 'DD-MON-YY HH24:MI:SS')        AS Start_DATE, 
     to_char(t.resolved_date, 'DD-MON-YY HH24:MI:SS')       as closed_date, 
     to_char(t.start_date, 'MI') as start_date_min, 
     to_char(t.closed_date, 'MI') as closed_date_min, 
     to_char(t.start_date, 'HH24') as start_date_h, 
     to_char(t.closed_date, 'HH24') AS Closed_DATE_h 
FROM t 

與您的代碼的問題:
TO_TIMESTAMP(t.start_date, 'DD-MON-YYYY HH24:MI:SS') - 第一個參數是char所以Oracle轉換datetime列 「起始日期」 爲char使用會話NLS設置。它可能不同於你的面具'DD-MON-YYYY HH24:MI:SS'