I had a problem this afternoon that was giving me fits. The problem was how to take two dates and determine the number of days between them within a SELECT statement. It was harder than I thought it would be!
First off, the fields in the table are not date-type fields, but 8-byte character fields. The values are formatted like *ISO fields (without the dashes). For example, last Friday would be "20070112". So the first thing I had to do was convert that to an actual date. I did that like this:
The DATE() function returns a date from a value. The argument must be an expression that returns a value of one of the following built-in data types: a date, a timestamp, a character string, a graphic string, or any numeric data type.
So now to figure out the number of days that have elapsed since the Order Date. To get today's date as a date-type field, I used the CURDATE() function. Simple enough. Now just subtract one from the other to get the duration.
All done, right? WRONG!
As it turns out, the result from this calculation is a number that represent the Months and Days since Order Date! So for December 15, 2006, as the order date, the result of the above calculation was 104. Not 104 days ago, but 1 month and 4 days ago! Useless!
What to do? What to do? I know! What if I could convert the date fields to integers that represent the number of days since some date a long, long time ago? Then, when I subtracted them, my result would be a number of days. That's where the DAYS() function comes in!
The DAYS() function (not to be confused with the DAY() function) returns an integer representation of a day that is the number of days between January 1, 0001, and the date passed in as the argument. Apply that to both dates, then subtract:
Again using 12/15/2006 for Order Date, this calculation returns 35, the answer I was looking for!
I hope this helps someone else who may run across this problem one day.
Thanks,
Michael
First off, the fields in the table are not date-type fields, but 8-byte character fields. The values are formatted like *ISO fields (without the dashes). For example, last Friday would be "20070112". So the first thing I had to do was convert that to an actual date. I did that like this:
Code:
DATE(SUBSTR(OrderDate, 1, 4) || '-' || SUBSTR(OrderDate, 5, 2) || '-' || SUBSTR(OrderDate, 7, 2))
So now to figure out the number of days that have elapsed since the Order Date. To get today's date as a date-type field, I used the CURDATE() function. Simple enough. Now just subtract one from the other to get the duration.
Code:
CurDate() - DATE(SUBSTR(OrderDate, 1, 4) || '-' || SUBSTR(OrderDate, 5, 2) || '-' || SUBSTR(OrderDate, 7, 2)) as Duration
As it turns out, the result from this calculation is a number that represent the Months and Days since Order Date! So for December 15, 2006, as the order date, the result of the above calculation was 104. Not 104 days ago, but 1 month and 4 days ago! Useless!
What to do? What to do? I know! What if I could convert the date fields to integers that represent the number of days since some date a long, long time ago? Then, when I subtracted them, my result would be a number of days. That's where the DAYS() function comes in!
The DAYS() function (not to be confused with the DAY() function) returns an integer representation of a day that is the number of days between January 1, 0001, and the date passed in as the argument. Apply that to both dates, then subtract:
Code:
DAYS(CurDate()) - DAYS(DATE(SUBSTR(OrderDate, 1, 4) || '-' || SUBSTR(OrderDate, 5, 2) || '-' || SUBSTR(OrderDate, 7, 2))) as Duration
I hope this helps someone else who may run across this problem one day.
Thanks,
Michael
Comment