[英]Retrieve Year from string date in Oracle SQL
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. 我想以YYYY的形式从这些系列中检索年份。 I already tried using the following line of SQL code: 我已经尝试使用下面的SQL代码行:
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 ORA-01830:日期格式图片在转换整个输入字符串之前结束
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.