Jim
Jim

Reputation: 51

SQL Query gives missing parenthesis error

I have been working on this for a few days and cannot figure out where the error is. The query is:

Select distinct p.PkgID, p.PkgName, p.PkgCost, c.CustFName || ' ' || c.CustLName as   "CUSTOMERNAME" 
FROM Subscription s, Package p, Customer c
WHERE p.PkgID = s.PkgID
AND c.CustID = s.CustID     
AND to_date 
 (s.StartDate, 'Mon DD, YYYY')=
to_date 
 (&StartDate, 'Mon DD, YYYY')
ORDER BY p.PkgID;

I keep getting:

SQL Error: ORA-00907: missing right parenthesis 00907. 00000 - "missing right parenthesis".

I cannot find the issue. Any help would be much appreciated. Here is what the table and insert statement looks like.

CREATE TABLE SUBSCRIPTION
(
SubID NUMBER(5) NOT NULL CONSTRAINT SUBSCRIPTION_SubID_PK PRIMARY KEY,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
CustID NUMBER(5) NOT NULL,   
PkgID NUMBER(5) NOT NULL  
 );

ALTER TABLE SUBSCRIPTION ADD CONSTRAINT SUBSCRIPTION_CustID_FK FOREIGN KEY (CustID)     REFERENCES CUSTOMER (CustID);
ALTER TABLE SUBSCRIPTION ADD CONSTRAINT SUBSCRIPTION_PkgID_FK FOREIGN KEY (PkgID) REFERENCES PACKAGE (PkgID);

INSERT INTO SUBSCRIPTION
VALUES (010, to_date('Jan 01, 2010 11:30','Mon DD, YYYY hh24:mi'), to_date('Jan 01,   2013 14:30','Mon DD, YYYY hh24:mi'), 00001, 10101);
INSERT INTO SUBSCRIPTION
VALUES (015,to_date('Mar 01, 2012 17:00','Mon DD, YYYY hh24:mi'), to_date('Dec 05, 2012 17:00','Mon DD, YYYY hh24:mi'), 00002, 20202);

As a side note, I did try it this way as well:

Select distinct p.PkgID, p.PkgName, p.PkgCost, c.CustFName || ' ' || c.CustLName as   "CUSTOMERNAME" 
FROM Subscription s
        Left OUTER JOIN Package p on p.PkgID = s.PkgID
    Left OUTER JOIN Customer c on c.CustID = s.CustID       
WHERE to_date (s.StartDate, 'Mon DD, YYYY')=
to_date (&StartDate, 'Mon DD, YYYY')    
ORDER BY p.PkgID;

Upvotes: 1

Views: 989

Answers (1)

Ben
Ben

Reputation: 52853

The input to TO_DATE() is two strings; even though you're using SQL*Plus substitution variables, you still need to enforce this.

Change

AND to_date(s.StartDate, 'Mon DD, YYYY') = to_date(&StartDate, 'Mon DD, YYYY')

to

AND to_date(s.StartDate, 'Mon DD, YYYY') = to_date('&StartDate', 'Mon DD, YYYY')

Are you sure s.StartDate is a character in this format? It sounds like it's already a date (I hope), in which case you don't need the TO_DATE() but it might be worth using TRUNC() on the date to remove the time portion.


To edit my answer based on the comments I don't believe you've changed this in the correct way. It should be:

AND trunc(s.StartDate) = to_date('&StartDate', 'Mon DD, YYYY')

Note the TRUNC() and the ' around '&StartDate'

Upvotes: 5

Related Questions