views:

341

answers:

1

I'm developing a multi-database system.
I want the difference between two dates in seconds.

In SQL Server I got:
DATEDIFF(second,stardate,enddate)
In MySql:
TIME_TO_SEC(TIMEDIFF(stardate,enddate))

My question:
Does Sql Ansi have functions to manage DateTime values?
i.e.: There are datetime functions generic for all databases?

+1  A: 

According to SQL:1999, date1-date0 should give you a value of type INTERVAL, a struct from which you should be able to extract YEAR, MONTH, DAY, etc.

I've never used it and I don't think it's widely supported (though I may not be up-to-date). If you're doing time arithmetic in the database layer and you want to be cross-DBMS compatible the usual solution is simply to use integer timestamps (of whatever resolution, but Unix time is common) and plain old integer arithmetic which is completely reliable cross-platform.

bobince
More or less, hence the upvote. INTERVAL values (found in IBM Informix Dynamic Server for one) can be of one of two classes - either YEAR TO MONTH or DAY TO SECOND. Indeed, you can have an INTERVAL YEAR TO YEAR, or MONTH TO MONTH, or DAY TO MINUTE, or HOUR TO SECOND, etc, including just SECOND.
Jonathan Leffler