Link to home
Start Free TrialLog in
Avatar of patriotpacer
patriotpacer

asked on

Oracle - Date Question

This is a follow up to this question.
https://www.experts-exchange.com/questions/28287097/Oracle-Calculate-Business-Days.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

Avatar of Sean Stuber
Sean Stuber

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');
Avatar of patriotpacer

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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,
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.
thank you!
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
https://www.experts-exchange.com/questions/28287225/Sub-Query-Error.html

I'm sort of outside the system and can't create functions.
>>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
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.