简体   繁体   中英

Get month number from month name

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

Check your oracle language, it seems to be in another.

Because im trying with this code:

select to_char(to_date('ABR 1990','MONTH yyyy'), 'mm') as MONTH from dual;

And i get:

结果

Note: My native language is Spanish.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM