2017-06-20 25 views
0

查詢Oracle中的時區的數據類型我有以下存儲在表如何使用java時間格式

create table sample(id number,last_modified_dt timestamp with time zone); 

select last_modified_dt from sample; 

last_modified_dt 
----------------- 
19-06-17 09:36:28.100452000 AM +00:00 

我的前端的Node.js應用程序嘗試使用下面的查詢時區數據的時間戳格式,

'2017-06-19T09:36:28Z' (java timestamp format) 


select last_modified_dt from sample where last_modified_dt = '2017-06-19T09:36:28Z' 

我嘗試使用

select last_modified_dt from sample where last_modified_dt = TO_TIMESTAMP('2017-06-19T09:36:28Z','YYYY-MM-DD"T"HH24:MI:SS"Z"'); 

但它返回空的結果,顯然我知道有些東西不見了,請你幫我解決。

回答

0

不是最好的解決方案,而是一個非常快速的解決方案是 如果你總是拿到的時候在格式

select last_modified_dt from sample 
    where TRUNC(last_modified_dt) = 
      TRUNC(to_date (regexp_replace ('2017-06-19T09:36:28Z', 
        '[a-zA-Z]',' '), 
      'rrrr-mm-dd hh:mi:ss')) -- TRUNC can be added if only date is important for matching and time is not important. 

REGEXP_REPLACE會從你從Java得到時間戳刪除任何字符的出現。

a-ZA-Z將確保所有英文字母都被刪除。

如果DB格式dd-mm-yyyy,只有當日期被存儲爲字符

select last_modified_dt from sample 
where TRUNC(last_modified_dt) = 
     TRUNC(to_date (regexp_replace ('2017-06-19T09:36:28Z', 
       '[a-zA-Z]',' '), 
     'rrrr-mm-dd hh:mi:ss')) 
+0

嗯,謝謝你的評論,但它仍然是不工作作爲數據庫格式是DD-MM-YYYY和輸入是yyyy-mm-dd。有沒有辦法將時間戳轉換爲日期格式? –

+0

@AnishGopinath我不認爲這有什麼區別,你從你的SQL客戶端看到的可能有不同的格式。當你使用'TO_DATE'轉換你的輸入時,它應該與oracle相匹配,沒有問題。 'dd-mm-yyyy'可能是您在SQL客戶端上根據您的設置看到的東西。 –

+0

monda:即時獲取ORA-01830:日期格式圖片在轉換整個輸入字符串錯誤之前結束 –

相關問題