Skip to content
Advertisement

Extract month from number of days in a year

I am trying to extract month from number of day in year in Oracle SQL, for instance day 32 is february.

I do not have permission to use procedure, so I have to do it just using select.

Any help?

Advertisement

Answer

You get to the solution as follows. First convert the day X into a date and extract the month from it again. Replace the value 150 with the day of the year you are looking for.

For Month as Number:

select extract(month from (to_date(150,'DDD'))) from dual;

Works also with the year. Format of the to-date must be simply changed to match.

select extract(month from (to_date('150 2022','DDD YYYY'))) from dual;

For Month as Month Name:

select to_char(to_date(150,'DDD'),'Month') from dual;
select to_char(to_date('150 2022','DDD YYYY'),'Month') from dual;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement