Oracle - Extract last month name from sysdate - sql

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>

Related

Get the month name from a timestamp column [duplicate]

How to fetch month name from a given date in Oracle?
If the given date is '15-11-2010' then I want November from this date.
select to_char(sysdate, 'Month') from dual
in your example will be:
select to_char(to_date('15-11-2010', 'DD-MM-YYYY'), 'Month') from dual
Try this,
select to_char(sysdate,'dd') from dual; -> 08 (date)
select to_char(sysdate,'mm') from dual; -> 02 (month in number)
select to_char(sysdate,'yyyy') from dual; -> 2013 (Full year)
to_char(mydate, 'MONTH') will do the job.
In Oracle (atleast 11g) database :
If you hit
select to_char(SYSDATE,'Month') from dual;
It gives unformatted month name, with spaces, for e.g. May would be given as 'May '. The string May will have spaces.
In order to format month name, i.e to trim spaces, you need
select to_char(SYSDATE,'fmMonth') from dual;
This would return 'May'.
If you are trying to pull the value from a field, you could use:
select extract(month from [field_name])
from [table_name]
You can also insert day or year for the "month" extraction value above.
if you are taking system date:
--Full month name :
select to_char(trunc(sysdate,'MONTH'),'MONTH') as month from dual; --MARCH
--Short month name:
select to_char(trunc(sysdate,'MON'),'MON') as month from dual; --MAR
--Month number:
select to_char(trunc(sysdate,'MM'),'MM') as month from dual; --03
if you are taking a specific date:
--Full month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MONTH'),'MONTH') as month from dual; --MARCH
--Short month's name:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MON'),'MON') as month from dual; --MAR
--Month's number:
select to_char(trunc(to_date('11-03-2021','DD-MM-YYYY'),'MM'),'MM') as month from dual; --03
Try this
select to_char(SYSDATE,'Month') from dual;
for full name and try this
select to_char(SYSDATE,'Mon') from dual;
for abbreviation
you can find more option here:
https://www.techonthenet.com/oracle/functions/to_char.php

Convert 'DD-MM-YY' to 'Month Year' oracle

I have a date a field that displays date in the format '19-JAN-20' I would like to display it as 'January 2019' using oracle database.
select '19-JAN-19' FROM DUAL
You need to use TO_CHAR to display in the required format:
select to_char(DATE '2019-01-20', 'Month YYYY') dt from dual;
DT
-------------
January 2019
select '19-JAN-19' FROM DUAL
There are couple of issues with that statement:
'19-JAN-19' is a string, not a date. Always use TO_DATE to explicitly convert it to date, or stick to ANSI date literal like I used in my demo.
Do not use two-digit YY representation for year, that's the reason Y2K bug was introduced. Always use YYYY.
Looks like a nested TO_CHAR, if "date" value you mentioned - 19-JAN-20 is stored as a string:
SQL> with test (col) as
2 (select '19-JAN-20' from dual)
3 select
4 to_char(
5 to_date(col, 'dd-mon-yy', 'nls_date_language = english'),
6 'fmMonth yyyy', 'nls_date_language = english'
7 ) result
8 from test;
RESULT
--------------
January 2020
SQL>
If your database speaks English, you can omit the NLS_DATE_LANGUAGE part (mine speaks Croatian so I included it).
However, if it is a date you'd just want to display differently, then:
(Just to avoid NLS_DATE_LANGUAGE in TO_DATE):
SQL> alter session set nls_date_language = 'english';
Session altered.
Default format in my database:
SQL> select sysdate from dual;
SYSDATE
--------
08.06.20
Alter session to desired format:
SQL> alter session set nls_date_format = 'fmMonth yyyy';
Session altered.
SQL> select sysdate from dual;
SYSDATE
--------------
June 2020
Or, apply TO_DATE function with desired format mask:
SQL> select to_char(sysdate, 'fmMonth yyyy') result from dual;
RESULT
--------------
June 2020
SQL>

How to get last day data of previous 12 months from current month

There is a table in oracle named emp_table with emp_name(name of employees), emp_att (values are P or A only), and date (in timestamp). I want to view emp_att values for last day(end date of months) of previous 12 months from current month. Please share a proper query to get that.
Assuming that last date in your case may not be the last date of the month, and last date is same for all the employees for all the months:
SELECT * FROM
(SELECT A.*,
LAST_VALUE(EMP_DATE) OVER(PARTITION BY TO_CHAR(EMP_DATE,'MON-YYYY') ORDER BY TO_CHAR(EMP_DATE,'MON-YYYY')) LAST_DT
FROM EMP_DATE A
WHERE
EMP_DATE BETWEEN ADD_MONTHS(TO_CHAR(SYSDATE,'DD-MON-YYYY'), -12) AND TO_CHAR(SYSDATE,'DD-MON-YYYY'))
WHERE EMP_DATE=LAST_DT
the last_value function will give last working date
for particular month
where condition in select statement will restrict dates to last one year from present date you can change sysdate with your own date
Something like this?
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> with emp_table (emp_name, emp_att, cdate) as
2 (select 'Scott', 'P', date '2019-03-31' from dual union all
3 select 'Scott', 'A', date '2019-04-30' from dual union all
4 select 'Scott', 'A', date '2019-05-18' from dual union all --> not the last day in May
5 select 'Scott', 'P', date '2019-05-31' from dual union all
6 select 'Scott', 'A', date '2019-06-13' from dual --> not the last day in June
7 )
8 select emp_name, emp_att, cdate
9 from emp_table
10 where cdate >= add_months(sysdate, -12)
11 and cdate = last_day(cdate);
EMP_N E CDATE
----- - ----------
Scott P 31.03.2019
Scott A 30.04.2019
Scott P 31.05.2019
SQL>

Formatting a date in Oracle to show month and day

I have the date as below
SELECT TO_CHAR(SYSDATE, 'DDD') FROM dual;
result for above query is : 117
I need to convert this 117 to month and day. anyone knows how to perform this in PL SQL
the expected result is 04-27
Change 'DDD' to 'MM-DD'.
Try this:
SELECT TO_CHAR(SYSDATE, 'MM-DD')
FROM dual;
OUTPUT:
04-27
Demo:
http://sqlfiddle.com/#!4/622055/7
EDITED:
DDD shows Number of Days from the first of a year.
For Example:
SELECT TO_CHAR(TO_DATE('2018-01-01','YYYY-MM-DD'), 'DDD')
FROM dual;
gives output 001 since it is the First Day of year 2018.
So to break 117 you need the year.
Then you can use this query.
SELECT TO_CHAR(TO_DATE('2018-01-01','YYYY-MM-DD') + column_name - 1, 'MM-DD')
FROM dual;
Example Query:
SELECT TO_CHAR(TO_DATE('2018-01-01','YYYY-MM-DD') + 117 - 1, 'MM-DD')
FROM dual;
OUTPUT:
04-27
Demo:
http://sqlfiddle.com/#!4/622055/37
If you have "117" and want to convert it to a date, you'll have to use a little bit (but really - just a little bit) of arithmetics. As 'DDD' represents number of days since the 1st of current year, add it to ... well the 1st of current year and apply appropriate format mask to convert it to date. I subtracted "1" as you want to get yesterday.
For example:
SQL> select to_char(sysdate, 'ddd') from dual;
TO_
---
117
SQL> select to_date(trunc(sysdate, 'yyyy') + 117, 'dd.mm.rrrr') - 1 resul
2 from dual;
RESULT
----------
27.04.2018
SQL>
Or, using the format you specified (MM-DD):
SQL> select to_char(trunc(sysdate, 'yyyy') + 117 - 1, 'mm-dd') result
2 from dual;
RESUL
-----
04-27
SQL>

Specific day- sql oracle

How can I find a specific day no matter the year or date in sql oracle.
as an example
i have the folowing dates in and table
id date
1 2013-02-03
2 2013-01-04
3 2012-06-13
I want to find all the ids with the day= 13
I've did this already but it doesn't work
select id
from table
where dayofmonth(date) = 13
this works in mysql but not in oracle.
Can any of you help me?
You can either use to_char() + to_num()
select to_number(to_char(sysdate, 'DD')) from dual
or use the extract() function:
select extract (day from sysdate) from dual
EDIT:
To get all rows from a table whose day is 13, just use EXTRACT in the WHERE clause:
create table my_data as
(select 1 pk, to_date('2013-01-13', 'YYYY-MM-DD') as my_date from dual union all
select 1 pk, to_date('2013-01-14', 'YYYY-MM-DD') as my_date from dual union all
select 1 pk, to_date('2013-02-13', 'YYYY-MM-DD') as my_date from dual);
select * from my_data
where extract(day from my_date) = 13;