If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > add two date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-05-11, 09:50
csckid csckid is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
add two date

how do you add two date in postgresql?
I tried this, but this gives an error
Code:
select finish_time + arrival_time from file_info
Code:
ERROR:  operator is not unique: time without time zone + time without time zone
LINE 1: select finish_time + arrival_time from file_info
                           ^
HINT:  Could not choose a best candidate operator. You might need to add explicit type casts.
Subtraction of two dates works
Code:
select finish_time - arrival_time from file_info
Reply With Quote
  #2 (permalink)  
Old 06-05-11, 14:23
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,677
And what should the result of adding two dates be?

For subtracting this yields the difference in days.

What do you expect as the result of Sunday, June 5th plus Saturday, June 4th?
Reply With Quote
  #3 (permalink)  
Old 07-07-11, 02:30
csckid csckid is offline
Registered User
 
Join Date: Jun 2011
Posts: 3
finish_time , arrival_time these are time without time zone.

finish_time "05:01:01"
arrival_time "03:02:02"

I would expect the answer to be 08:03:03
Reply With Quote
  #4 (permalink)  
Old 07-07-11, 03:04
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,677
Quote:
Originally Posted by csckid View Post
finish_time , arrival_time these are time without time zone.

finish_time "05:01:01"
arrival_time "03:02:02"

I would expect the answer to be 08:03:03
Sorry I didn't see that those were not columns of type date (mainly because you wrote "how can I add two dates" )

Unfortunately you can only add an interval to a time value. But with a little trick you can "convert" the time into an interval:

Code:
SELECT finish_time + (interval '1' second) * extract(epoch from time arrival_time)
FROM your_table
This works as follows:

extract(epoch from time arrival_time) converts the time into seconds. Those seconds are then "converted" to an interval by multiplying a one second interval with them. The resulting interval can then be added to the date value.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On