views:

24

answers:

4

I have a table that has a datetime column and I need to retrieve records with a date within the previous hour, day, week, month etc.

One constraint is that I need to support Oracle, MySQL, DB2 and SQL SERVER. Can I achieve this with one SQL query only?

+1  A: 

Since they all can handle datetimes differently, I'm going to guess you can't. You might be able to separate out the platform-specific stuff and swap it when the platform changes.

If you tell us more about this problem, we might be able to suggest something in more detail.

FrustratedWithFormsDesigner
+1: My sentiments exactly
OMG Ponies
A: 

The ANSI SQL funtion would be datediff(). But I doubt all (especially older) SQL dialects support it.

Frank
IIRC, DATEDIFF is not supported by Oracle or DB2
OMG Ponies
A: 

This sounds like a good reason to start up on a multi-tiered RDBMS-based-approach to retrieving data.

Raj More
+1  A: 

User-defined functions. Write your own DATEDIFF-equivalent for each RDBMS and abstract away the differences.

dan04
That may work across systems, but on some systems (I'm thinking SQL Server here), this approach will result in a performance hit.
eksortso