简体   繁体   English

如何修复 Oracle 中的日期格式问题?

[英]How to fix date format problem in Oracle?

I need to run this query我需要运行这个查询

SELECT ADD_MONTHS (
    TRUNC (TO_DATE (i.list_member, 'dd-mm-yyyy'),'MM'), 1 * LEVEL - 13) month
    FROM   DUAL

Where i.list_member is a VARCHAR2, which is a result of a select and the format is like mm-yyyy (09-2019)其中i.list_member是 VARCHAR2,这是选择的结果,格式类似于 mm-yyyy (09-2019)

When I run the query, I get当我运行查询时,我得到

SQL error [1841] [22008]: ORA-01841: the (complete) year must be between -4713 and +9999, and be different from 0

The problem is the date format, could someone help to solve it?问题是日期格式,有人可以帮忙解决吗?

i.list_member is a VARCHAR2, which is a result of a select and the format is like mm-yyyy (09-2019) i.list_member 是一个 VARCHAR2,它是一个选择的结果,格式类似于 mm-yyyy (09-2019)

To translate the date:翻译日期:

select add_months(to_date (i.list_member, 'mm-yyyy'), level - 13) month
from dual -- you should be selecting from a table here

Or if you want the month number of the translated date:或者,如果您想要翻译日期的月份数:

select extract(month from add_months(to_date (i.list_member, 'mm-yyyy'), level - 13)) month
from dual

Format mask should match data, ie格式掩码应该匹配数据,即

SQL> alter session set nls_date_format = 'dd.mm.yyyy';

Session altered.

SQL>     SELECT ADD_MONTHS (TRUNC (TO_DATE ('09-2019', 'mm-yyyy'), 'MM'),
  2                         1 * LEVEL - 13)
  3                month                     ^^^^^^
  4        FROM DUAL                         This is i.list_member     
  5  CONNECT BY LEVEL <= 12
  6  /

MONTH
----------
01.09.2018
01.10.2018
01.11.2018
01.12.2018
01.01.2019
01.02.2019
01.03.2019
01.04.2019
01.05.2019
01.06.2019
01.07.2019
01.08.2019

12 rows selected.

SQL>

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

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