I just finished hashing out this SQL query for a project and just had to share it.
A popular function to do on sites is to find the difference in 2 dates. In ASP, you can use the DateDiff function to find this value. However I wanted a way to select it straight from the database as there were already too many ASP function taking place on the page.
For those of you unfamiliar with SQL, there exists the SUM function where you can add the values of database fields right in the SQL query and select is as you would a normal field. In MySQL, there exists a TO_DAYS function that, when supplied with a date parameter, will spit back the number of days from day 0 (which is sometime in 1582). Using both of these in confunction will yield the desired integer when you have to find the difference between two dates. Here is the SQL query I used (although i removed the table names and such and made it more general):
SELECT SUM((TO_DAYS(Finish_Date) – TO_DAYS(Start_Date))) AS DiffDays FROM your_table
Using this query (either direct from your script or via a database view) will output an integer into the recordset field “DiffDays”. Then you just have to call it on your page and you are done.
But what if you have a great many records and have to find one with the largest difference in dates? Not a problem. Try this query:
SELECT SUM((TO_DAYS(Finish_Date) – TO_DAYS(Start_Date))) AS DiffDays FROM your_table ORDER BY DiffDays DESC LIMIT 1
This will output only one record, and that record will have the largest date difference of all the records in your table. If you want to find the smallest difference, swap out DESC for ASC and you will get the smallest difference.
For more info on the MySQL date object, check out the MySQL Reference pages.
Hope this helps, and happy selecting.
Cheers










I was looking for the recipe on how to make orange flavored ice blended beverages (hope that description wards off any lawyers from Orange Julius) and came across the blog of Paul Mayne. Here is his recipe from his site (see below). I tried this out and it tastes awesome. And its close enough to the actual product you get over-charged for at the mall that my son couldn’t tell the difference; and a more picky eater you will not find anywhere.