I have the following column of dates:
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 . . .
I would like to retrieve the year from these series in the form YYYY. I already tried using the following line of SQL code:
TO_DATE(TO_CHAR(EXPIRY_DAT, 'DD-MON-YY HH:MI:SS'), 'YYYY'
although it returns me the error message:
ORA-01830: date format picture ends before converting entire input string
Use:
select extract(year from TO_DATE(SUBSTR(EXPIRY_DAT,1,9),'DD-Mon-YY')
from your_table
If all your dates are in one of the formats you showed, this could be enough:
-- 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.