Solved

first day and last day of the month

Posted on 2014-02-23
7
405 Views
Last Modified: 2014-02-25
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
Comment
Question by:NiceMan331
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:johnsone
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

by:NiceMan331
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;

Open in new window


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;

Open in new window


it return
01-12-14   and  31-12-14
instead of :
01-01-14  and 31-01-14
0
 
LVL 34

Accepted Solution

by:
johnsone earned 500 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; 

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:NiceMan331
ID: 39882349
excellent
0
 

Author Comment

by:NiceMan331
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 34

Expert Comment

by:johnsone
ID: 39886355
0
 

Author Comment

by:NiceMan331
ID: 39886387
ok
thanx
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now