Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# first day and last day of the month

Posted on 2014-02-23
Medium Priority
410 Views
hi
if i have 2  values of
v_year year &
v_m  month ,
in the form
how to get first & last day of given month & year
0
Question by:NiceMan331
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3

LVL 35

Expert Comment

ID: 39880617
Converting to a date, gives you the first day of the month, and using the LAST_DAY function gives you the last day, so something like this:

select to_date(v_m || v_year, 'mmyyyy') first_day_of_month,
last_day(to_date(v_m || v_year, 'mmyyyy')) last_day_of_month
from dual;
0

Author Comment

ID: 39881847
strange

i have this funtion

``````create or replace FUNCTION CURR_m RETURN NUMBER IS
Y number(2);
BEGIN
select period_no into Y from period_detail where year= curr_y() and checked = -1;
RETURN Y;
END;
``````

select curr_m from dual;
it return  1  , correct
but when i use it like this

``````select to_date(curr_m || curr_y, 'mmyyyy') first_day_of_month,
last_day(to_date(curr_m || curr_y, 'mmyyyy')) last_day_of_month
from dual;
``````

it return
01-12-14   and  31-12-14
01-01-14  and 31-01-14
0

LVL 35

Accepted Solution

johnsone earned 2000 total points
ID: 39882232
That is because your curr_m is 1 and not 01.  The dates you are getting back are:

December, 01 0014 00:00:00+0000 and December, 31 0014 00:00:00+0000

In a way I would expect that, but I would rather see error that the input doesn't match the supplied format.

Assuming that the 2 variables are numbers and that the year should never be below 1000, then this should work:

``````SELECT To_date(To_char(curr_m, 'fm00')
|| curr_y, 'mmyyyy'),
Last_day(To_date(To_char(curr_m, 'fm00')
|| curr_y, 'mmyyyy'))
FROM   dual;
``````
0

Author Comment

ID: 39882349
excellent
0

Author Comment

ID: 39886304
before i accept your answer , could you please explain to me what is the use of  "fm00" ?
just for knowledge
thanx
0

LVL 35

Expert Comment

ID: 39886355
0

Author Comment

ID: 39886387
ok
thanx
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
###### Suggested Courses
Course of the Month7 days, 1 hour left to enroll