Solved

Oracle - Date Question

Posted on 2013-11-06
11
508 Views
Last Modified: 2013-11-07
This is a follow up to this question.
http://www.experts-exchange.com/Database/Oracle/Q_28287097.html

Two queries below.  The FIRST one works - gives me 8 days.  The second does not work.

My date from my database appears as shown in the second.   How do I fix the SECOND so that it works?

FIRST - Works
SELECT COUNT(*) FROM (    SELECT TRUNC(SYSDATE) + LEVEL  d FROM DUAL CONNECT BY TO_DATE('2013-10-25', 'yyyy-mm-dd') + LEVEL <= TRUNC(SYSDATE)) WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')

Open in new window


SECOND - Does NOT work
SELECT COUNT(*) FROM (    SELECT TRUNC(SYSDATE) + LEVEL  d FROM DUAL CONNECT BY TO_DATE('25-OCT-13', 'yyyy-mm-dd') + LEVEL <= TRUNC(SYSDATE)) WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')

Open in new window

0
Comment
Question by:patriotpacer
[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
  • 6
  • 4
11 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 39628207
you kept the sysdate in the select and not just the connect by


the first query you have is actually wrong too, it just happens to be returning the correct answer today, but won't reliably do so


also,  if you use TO_DATE,  make sure your formats match

TO_DATE('25-OCT-13', 'yyyy-mm-dd')    -  you have 25 for yyyy,  Oct for mm and 13 for dd
In otherwords,  October 13th in the year 0025  (about 2000 years ago)


I recommend using the second form of the query from your original question,  it makes it easier to maintain.
make sure start_date is always less than or equal to end_date


SELECT COUNT(*)
  FROM (    SELECT start_date + LEVEL d
              FROM (SELECT DATE '2013-10-25' start_date, trunc(sysdate) end_date FROM DUAL)
        CONNECT BY start_date + LEVEL <= end_date)
 WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun');
0
 

Author Comment

by:patriotpacer
ID: 39628228
Thanks.  You'll have to excuse my oracle syntax stupidity.  

My problem is that my date shows like this when I just do a general query:  '25-OCT-13'

What modifications do I need to get the query to work?  Plugging in this date format gives me this error.


ORA-01843: not a valid month
01843. 00000 -  "not a valid month"
*Cause:    
*Action:
Error at Line: 14 Column: 33
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39628232
to_date('25-OCT-13','dd-MON-RR')
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 74

Expert Comment

by:sdstuber
ID: 39628244
also note,  if you are using actual DATE values then there is no format,   25-OCT-13  is a string/text format, but is not actually a date.  It's simply a string that you, as a human, can interpret as a date.


If you have DATE columns or DATE variables, then you don't need (or want) to do conversions on them
0
 

Author Comment

by:patriotpacer
ID: 39628265
I'm trying to make this a sub-query column.  

The field I need as STARTING date is called posting_date.  Running the SQL below as a sub-query I get this error.  

ORA-00904: "POSTING_DATE": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 24 Column: 69


(SELECT COUNT(*) FROM (    SELECT start_date + LEVEL d FROM (SELECT to_date(posting_date,'dd-MON-RR') start_date, trunc(sysdate) end_date FROM DUAL) CONNECT BY start_date + LEVEL <= end_date) WHERE TO_CHAR(d, 'Dy') NOT IN ('Sat', 'Sun')) as D1,
0
 

Author Comment

by:patriotpacer
ID: 39628281
Just hit me that I'm doing scope creep.  You technically answered what I was asking.  

I'll ask about my subquery in a new question.
0
 

Author Closing Comment

by:patriotpacer
ID: 39628282
thank you!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39628284
post your real query without this subquery and I'll figure out how to get it to fit.

You may want to create a function to do this
0
 

Author Comment

by:patriotpacer
ID: 39628327
http://www.experts-exchange.com/Database/Oracle/Q_28287225.html

I'm sort of outside the system and can't create functions.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39629903
>>My problem is that my date shows like this when I just do a general query:  '25-OCT-13'
the default display format of a date has nothing whatever to do with the way dates are stored

you could change the default date display format to: 25 October, 2013 17:28:36

but you would not have to change every sql query because of that
0
 

Author Comment

by:patriotpacer
ID: 39630122
you could change the default date display format to: 25 October, 2013 17:28:36


Thanks!  You guys are a tremendous help.  Can't tell you how much I appreciate the comments.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dbms_crypto.decrypt   errors out 6 57
error in my cursor 5 57
subtr returning incorrect value 8 91
Oracle database T-1 Setup 7 44
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

738 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