File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
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
author & internet detective

Joined: May 26, 2003
Posts: 32819

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, 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
Sonny Gill LinkedIn
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
It's not a secret anymore!