Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 533
  • Last Modified:

Oracle - Date Question

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
patriotpacer
Asked:
patriotpacer
  • 6
  • 4
1 Solution
 
sdstuberCommented:
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
 
patriotpacerAuthor Commented:
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
 
sdstuberCommented:
to_date('25-OCT-13','dd-MON-RR')
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sdstuberCommented:
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
 
patriotpacerAuthor Commented:
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
 
patriotpacerAuthor Commented:
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
 
patriotpacerAuthor Commented:
thank you!
0
 
sdstuberCommented:
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
 
patriotpacerAuthor Commented:
http://www.experts-exchange.com/Database/Oracle/Q_28287225.html

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now