[英]Retrieve Year from string date in Oracle SQL
我有以下日期列:
EXPIRY_DAT 31-Oct-17 31-Oct-17 18-Nov-18 12:11:10 31-Dec-12 31-Oct-17 31-Oct-17 18-Nov-18 12:11:10 31-Dec-12 31-Oct-17 31-Oct-17 20-Jul-18 19:20:33 31-Oct-18 31-Oct-18 11-Aug-19 21:52:56 31-Dec-12 . . .
我想以YYYY的形式從這些系列中檢索年份。 我已經嘗試使用下面的SQL代碼行:
TO_DATE(TO_CHAR(EXPIRY_DAT, 'DD-MON-YY HH:MI:SS'), 'YYYY'
盡管它返回了錯誤消息:
ORA-01830:日期格式圖片在轉換整個輸入字符串之前結束
采用:
select extract(year from TO_DATE(SUBSTR(EXPIRY_DAT,1,9),'DD-Mon-YY')
from your_table
如果您所有的日期都采用您顯示的格式之一,那么就足夠了:
-- test case
with dateTable(EXPIRY_DAT) as (
select '31-Oct-17' from dual union all
select '31-Oct-17' from dual union all
select '18-Nov-18 12:11:10' from dual union all
select '31-Dec-12' from dual union all
select '31-Oct-17' from dual union all
select '31-Oct-17' from dual union all
select '18-Nov-18 12:11:10' from dual union all
select '31-Dec-12' from dual union all
select '31-Oct-17' from dual union all
select '31-Oct-17' from dual union all
select '20-Jul-18 19:20:33' from dual union all
select '31-Oct-18' from dual union all
select '31-Oct-18' from dual union all
select '11-Aug-19 21:52:56' from dual union all
select '31-Dec-12' from dual
)
-- query
select to_char(to_date(substr(EXPIRY_DAT, 1, 9), 'dd-mon-yy'), 'yyyy')
from dateTable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.