简体   繁体   English

如何从Oracle中的日期中提取月份数

[英]How to extract month number from date in Oracle

I have ID_BB_SECURITY column where the date value is stored in this column for example '20190801' .我有ID_BB_SECURITY列,其中日期值存储在此列中,例如'20190801'

I want to get month number from this field for example for August date i want to get 8.我想从这个字段中获取月份数,例如我想得到 8 的八月日期。

I tried below query but it throws an error 'literal does not match':我试过下面的查询,但它抛出一个错误“文字不匹配”:

select to_number(to_date(ID_BB_SECURITY),'mm') from BT_EXPORT

I am not sure if i have to ignore null values so as to avoid the error我不确定是否必须忽略空值以避免错误

If the value is a number or string then you can convert it to a date with an appropriate mask - which is what you are missing, and what is causing the error you are getting (as it's using your session's NLS_DATE_FORMAT setting, which apparently does not match the format of the data; but which you should not rely on anyway, as @MTO said in comments):如果该值是数字或字符串,那么您可以将其转换为具有适当掩码的日期 - 这就是您所缺少的,以及导致您收到错误的原因(因为它使用的是会话的NLS_DATE_FORMAT设置,这显然没有匹配数据的格式;但无论如何你都不应该依赖它,正如@MTO 在评论中所说的那样):

to_date(ID_BB_SECURITY, 'YYYYMMDD')

and then extract the month number from that:然后从中提取月份数

select extract(month from to_date(ID_BB_SECURITY, 'YYYYMMDD')) from BT_EXPORT

Or you could just use a substring:或者你可以只使用一个子字符串:

select to_number(substr(ID_BB_SECURITY, 5, 2)) from BT_EXPORT;

Those assume a fixed consistent format, which is always a risky assumption when using the wrong data type.那些假设是固定的一致格式,当使用错误的数据类型时,这总是一个冒险的假设。 Ans if it's a number they are doing an implicit conversion from number to string, which you could turn into an explicit conversion for greater clarity. Ans 如果它是一个数字,他们正在进行从数字到字符串的隐式转换,为了更清晰,您可以将其转换为显式转换。

If it's already a date - as it should be, of course - then you don't need the conversion:如果它已经是一个日期 - 当然应该是 - 那么你不需要转换:

select extract(month from ID_BB_SECURITY) from BT_EXPORT

If you have a number, you can use arithmetic to extract the month:如果你有一个数字,你可以使用算术来提取月份:

select mod(floor(20190801 / 100), 100)
from dual;

You could try converting the number date to a string, and then extracting the 5th and 6th characters:您可以尝试将数字日期转换为字符串,然后提取第 5 个和第 6 个字符:

SELECT
    SUBSTR(TO_CHAR(ID_BB_SECURITY), 5, 2) AS mm
FROM BT_EXPORT;

But, it would be much better for you to use a proper date column.但是,使用适当的日期列对您来说会好得多。 Then, you could use a less draconian method such as:然后,您可以使用不那么严厉的方法,例如:

SELECT
    TO_CHAR(ID_BB_SECURITY, 'mm') AS mm  -- assuming date
FROM BT_EXPORT;
select to_number(to_char(to_date('20190801', 'yyyymmdd'), 'mm')) from dual

select extract(month from to_date('20190801', 'yyyymmdd')) from dual

Try this one试试这个

select extract(month from to_date(ID_BB_SECURITY, 'YYYYMMDD')) from BT_EXPORT

This one convert number to date then extract month.这一个将数字转换为日期然后提取月份。

Your date column has the value stored in the following format "yyyymmdd" where您的日期列的值以以下格式“yyyymmdd”存储,其中

  • yyyy is the year yyyy 是年份
  • mm the month mm 月份
  • dd the day So in order to return the number value of the month (mm) we can do as follows: dd the day 所以为了返回月份的数值(mm)我们可以这样做:

    1: first transform the value from a number to a date using to_date(20190801,'yyyymmdd') 1:首先使用 to_date(20190801,'yyyymmdd') 将值从数字转换为日期

    2: get month using to_date operator to_char( to_date(20190801,'yyyymmdd'), 'mm') 2: 使用 to_date 运算符获取月份 to_char( to_date(20190801,'yyyymmdd'), 'mm')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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