File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes date calculation inside select clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "date calculation inside select clause" Watch "date calculation inside select clause" New topic

date calculation inside select clause

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208

I have two columns of type TIMESTAMP defined inside a table:
start_date and stop_date. Is there any way I can calculate
how many days between start_date and stop_date inside my SELECT
clause so that I do not have do it after I get these two values
from my database?

Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30537

In db2, there is a days() function. So you could easily subtract in your select clause. I don't remember if this is db2 specific though.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
arnel nicolas
Ranch Hand

Joined: Dec 16, 2003
Posts: 149
Also in MSSQL you can use DATEDIFF function.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

In Oracle, AFAIK, you can just subtract one date from the other to get the number of days

The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
Thanks a lot guys. I am using db2. I will try out you guys' suggestions.
I agree. Here's the link:
subject: date calculation inside select clause