Cross-platform SQL date difference function or query
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 SERVE开发者_如何学CR. Can I achieve this with one SQL query only?
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.
User-defined functions. Write your own DATEDIFF
-equivalent for each RDBMS and abstract away the differences.
This sounds like a good reason to start up on a multi-tiered RDBMS-based-approach to retrieving data.
The ANSI SQL function would be datediff()
. But I doubt all (especially older) SQL dialects support it.
精彩评论