13

Say I have column of type dateTime with value "2014-04-14 12:17:55.772" & I need to subtract seconds "2" seconds from it to get o/p like this "12:17:53".

2 Answers 2

27
select '2014-04-14 12:17:55.772'::timestamp - interval '2 seconds';

For greater flexibility it is possible to mutiply the interval

select '2014-04-14 12:17:55.772'::timestamp - 2 * interval '1 second';

If you want to truncate to the second

select date_trunc(
    'second', 
    '2014-04-14 12:17:55.772'::timestamp - interval '2 seconds'
);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I tried it & it's working fine but my with this approach I am facing group by clause issue. I updated the problem statement & descrption. Please have a look
created different thread related to this question stackoverflow.com/questions/23063314/…
6

Postgres does not have a dateTime data type. I assume you mean a timestamp.

You can subtract an "interval" with the desired length from that column:

select the_timestamp_column - interval '2' second
from the_table

More about intervals in the manual

More about the operators available for date and timestamp columns in the manual

3 Comments

Thanks. I tried it & it's working fine but my with this approach I am facing group by clause issue. I updated the problem statement & descrption. Please have a look
@user1298426: that is a completely new question now.
created different thread related to this question stackoverflow.com/questions/23063314/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.