[英]Get month number from month name
I have a varchar field coming out as: MON-YYYY.我有一个 varchar 字段显示为:MON-YYYY。
I need to extract the month number from the field.我需要从字段中提取月份编号。 Ex: APR 2017.
例如:2017 年 4 月。
Expected output: 04预计 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:当然,因为我们只有 12 个,我可以做一个 substring 和 case,但尝试使用以下方法一次完成:
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
有没有其他更简单的方法从 2017 年 4 月获得“04”
What about the EXTRACT function? 那EXTRACT函数呢?
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm 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 -如果您需要 integer 格式的结果 - 试试这个 -
select EXTRACT(Month FROM to_date(<month_name_column>,'mon')) from <table_name> select EXTRACT(Month FROM to_date(<month_name_column>,'mon')) 来自 <table_name>
example- select EXTRACT(Month FROM to_date('January','mon')) from dual示例- select EXTRACT(Month FROM to_date('January','mon')) 来自 dual
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.