简体   繁体   中英

Oracle - Extract last month name from sysdate

If the date in my record is '04-NOV-2021', for ex, I want to show 'October'.

select extract(MONTH FROM add_months(sysdate,-1)) from dual;

That code will give me the '10' for October, but how do I format it as 'October'?

One option is to use to_char with the right nls_language .

SQL>  select to_char(sysdate,'Month', 'nls_language=''english''' ) from dual ;

TO_CHAR(SYSDATE,'MONTH','NLS_LANGUAG
------------------------------------
November

SQL>

In your case

SQL> select to_char(to_date(to_char(add_months(sysdate,-1))),'Month', 'nls_language=''english''' ) from dual ;

TO_CHAR(TO_DATE(TO_CHAR(ADD_MONTHS(S
------------------------------------
October

Or

SQL> select to_char(add_months(sysdate,-1), 'fmMonth', 'nls_language="english"')  from dual ;

TO_CHAR(ADD_MONTHS(SYSDATE,-1),'FMMO
------------------------------------
October

SQL>

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