Question

I am trying to compute all the bookings from my database guests have made booking for longer than a certain amount of time. the arrive date and depart date are in booking table. I am working in sqlplus with oracle, and the error invalid identifies datediff pops up. Here is my attempt. I would also like to be able to display the date differnce for each booking alog with the print.

SELECT b.BOOKINGID, g.FORENAME, g.SURNAME 
FROM GUEST g, BOOKING b
WHERE g.GUESTID = b.GUESTID AND
DATEDIFF(day, b.DEPARTDATE, b.ARRIVEDATE) > 2;
Was it helpful?

Solution 2

In ORACLE DATEDIFF doesn't exist.
But in Oracle, you can directly use + or - operands between two dates.

So your requets could be : (EDIT to add diff value in select)

 SELECT b.BOOKINGID, g.FORENAME, g.SURNAME, 
 CASE WHEN (SYSDATE - b.DEPARTDATE) > 0 THEN CONCAT((SYSDATE - b.DEPARTDATE), ' days late')
 ELSE CONCAT((b.DEPARTDATE - SYSDATE),' days left')
 END AS "Booking Status"
 FROM GUEST g, BOOKING b
 WHERE g.GUESTID = b.GUESTID
 AND (SYSDATE - b.DEPARTDATE) > 2;

This query show you only bookings with at least 2 days of late.

EDIT

The query that does that you're looking for is : (Almost identical to that of Gordon Linoff made)

 SELECT b.BOOKINGID, g.FORENAME, g.SURNAME, (b.DEPARTDATE - b.ARRIVEDATE) as booking_duration
 FROM GUEST g, BOOKING b
 WHERE g.GUESTID = b.GUESTID
 AND (b.DEPARTDATE - b.ARRIVEDATE) > 2;

OTHER TIPS

Try the Oracle equivalent:

SELECT b.BOOKINGID, g.FORENAME, g.SURNAME 
FROM GUEST g join
      BOOKING b
      on g.GUESTID = b.GUESTID
where  (b.DEPARTDATE - b.ARRIVEDATE) > 2;

I also changed the query to use ANSI join syntax.

Note: this assumes that the dates do not have time stamps. If so, you might want:

where trunc(b.departdate) - trunc(b.arrivedate) > 2

to get the number of nights.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top