Solved

Oracle date question

Posted on 2014-02-05
11
483 Views
Last Modified: 2014-02-05
If
  to_char(:p_start_date, 'MM/DD/YYYY') > to_char(:p_end_date, 'MM/DD/YYYY')  then      

message(........)
( Stops the report)
End if;

I need help when the user runs the report between years like

start date- 01-jan-2013  to
end date- 01-jan-2014

Say the user has to run report always in the year range, ie between jan 2013 and dec 2013. How can I do that.
0
Comment
Question by:anumoses
[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
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
11 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39835699
if p_start_date and p_end_date are already dates, remove the TO_CHAR conversion, as it will "incorrect" results, from "human" point of view.

see this article to explanations:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html

if really you wanted to use TO_CHAR , then with a format like YYYYMMDD , YYYY-MM-DD , YYYY/MM/DD or the like, otherwise the "comparison" will not work according to the "values" of dates.

if you do DD/MM/YYYY or MM/DD/YYYY, it will first compar the months and days values before comparing the year values, as the resulting string is no longer a "date".

hope this clarifies
0
 
LVL 19

Accepted Solution

by:
regmigrant earned 84 total points
ID: 39835702
If
  to_char(:p_start_date, 'MM/DD/YYYY') > to_char(:p_end_date, 'MM/DD/YYYY') OR
  to_char(:p_start_date, 'YYYY') <> to_char(:p_end_date, 'YYYY')

Then

etc...
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39835705
First, should you be using to_char?

Assuming to_date is correct, I just make sure I get them in the right order and I don't have to worry about stopping the report:

Use MIN for the left side and MAX for the right side.

min(to_date(:p_start_date, 'MM/DD/YYYY'),to_date(:p_end_date, 'MM/DD/YYYY'))

and

max(to_date(:p_start_date, 'MM/DD/YYYY'),to_date(:p_end_date, 'MM/DD/YYYY'))
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 74

Expert Comment

by:sdstuber
ID: 39835707
you mean the user can pick any year they want but they must start on Jan 1 and end on Dec 31?

if so,  then change the prompt to only ask for the year.  Don't make them enter extra values that only serve to add error conditions.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39835723
Anumoses, you have got to start treating dates as dates and not characters if you are going to performa any kind of date math. With character data 12/31/2013 is greater than 01/31/2014, which results in inaccurate comparisons.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39835753
If you let the user just pick the year, you could "parse" it like this for your query needed in your report:
select trunc(to_date('2013', 'yyyy'), 'Y') first_day_of_year,
       add_months(trunc(to_date('2013', 'yyyy'), 'Y'), 12) - 1 last_day_of_year
  from dual;

Open in new window

whereas '2013' should be replaced with the appropriate parameter ;-)

Apart from that, I totally agree with awking00: do NOT treat dates as strings in scenarios like yours!
0
 
LVL 6

Author Comment

by:anumoses
ID: 39835789
you mean the user can pick any year they want but they must start on Jan 1 and end on Dec 31?

if so,  then change the prompt to only ask for the year.  Don't make them enter extra values that only serve to add error conditions.

I cant ask only year. Since the report has to be run on the day and month also. This gives the user a graph for the date range.
0
 
LVL 6

Author Comment

by:anumoses
ID: 39835799
The message I am trying to give the user is here

The start date and end date has to be within the same year range.
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 83 total points
ID: 39835812
assuming p_start_date is a character string:

if to_char(to_date(:p_start_date, 'MM/DD/YYYY'),'YYYY') != to_char(to_date(:p_end_date, 'MM/DD/YYYY'),'YYYY') then
...


You could also do substr:
substr(:p_start_date,7,4) != substr(:p_end_date,7,4)
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 83 total points
ID: 39835850
and if it's a date:
if to_char(p_start_date, 'YYYY') <> to_char(p_end_date, 'YYYY') then ...
0
 
LVL 6

Author Closing Comment

by:anumoses
ID: 39836038
thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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

730 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