473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL - Date Math question

I have two SQL statements:

Statement #1

SELECT CARRIER_REFERENCE_NUMBER,
CURRENT_FLEET_SIZE,
trunc(Fleet_Size_Effective_From_Date),
trunc(Fleet_Size_Effective_To_Date)
FROM CAPS.CARRIER_FLEET_SIZE_HISTORY
WHERE CARRIER_REFERENCE_NUMBER = 481
AND trunc(Fleet_Size_Effective_TO_Date) >=
TO_DATE('24-FEB-2003') AND trunc(Fleet_Size_Effective_From_Date) <=
TO_DATE('24-FEB-2004')
ORDER BY CARRIER_REFERENCE_NUMBER,
Fleet_Size_Effective_From_Date,
Fleet_Size_Effective_To_Date,
CURRENT_FLEET_SIZE

************************************************** ****************************
Statement #2

SELECT CARRIER_REFERENCE_NUMBER,
CURRENT_FLEET_SIZE,
TRUNC(Fleet_Size_Effective_From_Date) AS START_DATE,
TRUNC(Fleet_Size_Effective_To_Date) AS END_DATE
FROM CAPS.CARRIER_FLEET_SIZE_HISTORY
WHERE CARRIER_REFERENCE_NUMBER = 481
AND TO_DATE(Fleet_Size_Effective_TO_Date,'DD-MON-YYYY') >=
TO_DATE(ADD_MONTHS(SYSDATE, -12),'DD-MON-YYYY')
AND TRUNC(Fleet_Size_Effective_From_Date) <=
TO_CHAR(SYSDATE,'DD-MON-YYYY')
ORDER BY CARRIER_REFERENCE_NUMBER,
Fleet_Size_Effective_From_Date,
Fleet_Size_Effective_To_Date,
CURRENT_FLEET_SIZE

Statement #1 returns 13 records....statement #2 returns 5 records.

Both Fleet_Size_Effective_TO_Date and Fleet_Size_Effective_From_Date
are of a DATE datatype. I am trying to restrict the query to the
sysdate as an end date, and sysdate - 12 months as a start date.
What's wrong with my date math?
Any input into this would be greatly appreciated.
Jul 19 '05 #1
1 8317
Alex wrote:
I have two SQL statements:

Statement #1

SELECT CARRIER_REFERENCE_NUMBER,
CURRENT_FLEET_SIZE,
trunc(Fleet_Size_Effective_From_Date),
trunc(Fleet_Size_Effective_To_Date)
FROM CAPS.CARRIER_FLEET_SIZE_HISTORY
WHERE CARRIER_REFERENCE_NUMBER = 481
AND trunc(Fleet_Size_Effective_TO_Date) >=
TO_DATE('24-FEB-2003') AND trunc(Fleet_Size_Effective_From_Date) <=
TO_DATE('24-FEB-2004')
ORDER BY CARRIER_REFERENCE_NUMBER,
Fleet_Size_Effective_From_Date,
Fleet_Size_Effective_To_Date,
CURRENT_FLEET_SIZE

************************************************** ****************************
Statement #2

SELECT CARRIER_REFERENCE_NUMBER,
CURRENT_FLEET_SIZE,
TRUNC(Fleet_Size_Effective_From_Date) AS START_DATE,
TRUNC(Fleet_Size_Effective_To_Date) AS END_DATE
FROM CAPS.CARRIER_FLEET_SIZE_HISTORY
WHERE CARRIER_REFERENCE_NUMBER = 481
AND TO_DATE(Fleet_Size_Effective_TO_Date,'DD-MON-YYYY') >=
TO_DATE(ADD_MONTHS(SYSDATE, -12),'DD-MON-YYYY')
AND TRUNC(Fleet_Size_Effective_From_Date) <=
TO_CHAR(SYSDATE,'DD-MON-YYYY')
ORDER BY CARRIER_REFERENCE_NUMBER,
Fleet_Size_Effective_From_Date,
Fleet_Size_Effective_To_Date,
CURRENT_FLEET_SIZE

Statement #1 returns 13 records....statement #2 returns 5 records.

Both Fleet_Size_Effective_TO_Date and Fleet_Size_Effective_From_Date
are of a DATE datatype. I am trying to restrict the query to the
sysdate as an end date, and sysdate - 12 months as a start date.
What's wrong with my date math?
Any input into this would be greatly appreciated.


Sysdate is today - not Feb, 24!
Also, the second query compares dates with characters (you
do use TO_CHAR(SYSDATE...)) and may thus not perform as well
--
Regards,
Frank van Bortel
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Mark Bram | last post by:
Howdy All! I am trying to write a very brief comparison of the Date and Math objects in terms of instance v static objects. What I have below is my best so far. Any criticisms or suggestions are...
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
6
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this...
3
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the...
1
by: Sam | last post by:
How do I convert Julian Date to Calendar Date in ASP.Net 1.1 based on following guideline found at Internet? To convert Julian date to Gregorian date: double JD = 2299160.5; double Z =...
12
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
1
by: Mtek | last post by:
Hi, We have a form where the user selects a date from a calendar, the date is in the format May 23, 2008. The date in the datebase is in the format 05212008. What we need to do is get the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.