Solved

Oracle - Date Question

Posted on 2013-11-06
11
501 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
  • 6
  • 4
11 Comments
 
LVL 73

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 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39628232
to_date('25-OCT-13','dd-MON-RR')
0
 
LVL 73

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
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: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 73

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

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.

Join & Write a Comment

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
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
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

762 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

18 Experts available now in Live!

Get 1:1 Help Now