I have a varchar field coming out as: MON-YYYY.
I need to extract the month number from the field. Ex: APR 2017.
Expected output: 04
Sure, since we have only 12, I can do a substring and case, but trying to do it in one shot using the below:
to_char(to_date(Period,'MON YYYY'),'MM') as Month
keeps running into errors -numeric found where non-numeric is expected etc.
What is wrong here? Is there any other simpler way to get "04" from APR 2017
What about the EXTRACT function?
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm
EXTRACT(MONTH FROM to_date(Period,'MON YYYY'),'MM') as Month
if you need result in integer format- Try this -
select EXTRACT(Month FROM to_date(<month_name_column>,'mon')) from <table_name>
example- select EXTRACT(Month FROM to_date('January','mon')) from dual
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.