Reputation: 431
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
Upvotes: 1
Views: 985
Reputation: 3015
Use:
select extract(year from TO_DATE(SUBSTR(EXPIRY_DAT,1,9),'DD-Mon-YY')
from your_table
Upvotes: 2
Reputation: 22949
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
Upvotes: 2